0

Lets say I have 3 Classes: A, Data, and B

I pass a variable from class A which sets that passed variable to a public static variable in class Data with Data's class method setData() and trying to get the same value (which i was assigned in class A) in class B using Data's class method getData().

methods in Data class are public and static

public class Data{

public static string nameD;

public static void setData(String name){ nameD = name; }

public static String getData(){ return nameD; }

}

public class A{

String nameA="Test";

Data.setData(nameA); }

public class B{

String nameB; nameB = Data.getData(); println(nameB); }

But gives null string in class B.

How can i do that.?

10
  • try putting nameB = Data.getData(); println(nameB); in the constructor. Commented Nov 16, 2013 at 20:20
  • This is very strange, what are you trying to do here. It looks very scary code. Where are your constructors? Commented Nov 16, 2013 at 20:20
  • This code won't compile. What are you trying to do ? Commented Nov 16, 2013 at 20:22
  • @MartínMarconcini i want to set a class member's value in another class 1 and trying to get same value in another class 2. Commented Nov 16, 2013 at 20:24
  • But do you realize that the code is a mess? Without seeing more code, it's very hard if not impossible to tell what your problem is. Who is creating the instances of A and B? Where are these classes created? In Which order? Etc. It's clear that what you're thinking that you're doing is not exactly what you wrote. Obviously setting a class member's value is trivial, and getting the value through a getter is also trivial, your problem lies in code we cannot see yet… Commented Nov 16, 2013 at 20:26

3 Answers 3

1

just call first class A to set the value for class Data then call class B to get the value inside the data.

ex:

public class A{
     String nameA="Test";
     public A() {
         Data.setData(nameA);
     }
 }

 public class B{
      String nameB; 
      B() {
         nameB = Data.getData(); 
         System.out.println(nameB); 

      }
 }

 public class Data{

    public static String nameD;

    public static void setData(String name){ nameD = name; }

    public static String getData(){ return nameD; }
  }

then if u made the following, you will got ur value new A(); new B();

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for solving my problem.
0

You could pass the object and have a static field in the object you pass.

Comments

0

It's not valid code, because you cannot execute a statement like Data.setData() outside of any method.

If you put this code in a method inside class A and then call this method in your main program in the correct order (before calling getData()), it will still be very confusing code, but you should get your desired result.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.