how to pass the value of a variable that is created in the class A in class B

I need to pass the value of a variable That Is created in a class A to a class B. I'm still a beginner and I can not figure out how to proceed. Could you give me a hand?

Answers


Please see below 2 options for passing a String value: through the constructor and through a public setter method.

class A {
  void method() {
    String s = "test";
    B b = new B(s);
    b.setValue(s);
  }
}

class B {
  private String value;

  B(String val) {
    value = val;
  }

  public void setValue(String val) {
    value = val;
  }
}

Need Your Help

appharbor WCF Service assembly is built by a runtime newer than the currently loaded runtime and cannot be loaded

c# .net wcf c#-4.0 appharbor

I am getting the following error when I try to run my WCF Service in appharbor. I use .NET Framework 4 in my local machine and it worked. I have disabled pre-compilation in appharbor so that it doe...

How do I seed the rand() function in Objective-C?

objective-c ios c random srand

Part of what I'm developing is a random company name generator. It draws from several arrays of name parts. I use the rand() function to draw the random name parts. However, the same "random" numbe...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.