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

How to dynamical specify table fields name in objectDB on any other DB

java jpa orm object-database objectdb

I am using objectdb with jpa2. I want to let my users specify the fields of my entity. How do I do that?

Object.toString() then, encode to UTF-8

java object utf-8

I am trying to implement a method that converts an object into an string, an then encodes this string to UTF-8 into a .txt. So far I am able to make it work, but the what is written into the output...

C# visual studio using resource files

c# winforms visual-studio-2010 resources

What I'm trying to do is adding two image files into the solution, so they will be compiled into the .exe, so I can load them right there instead of from an absolute path on the computer. I believe I

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.