5
votes

I need to get variable inString in other class. How can I do this?

public class main {
    public static StringBuffer inString;


    public static void main(String[] args) {
        inString = new StringBuffer("Our aim is to make a 15 realistic game, where grinding powerlines and doing a tailwhip isn't easy, like in the real world. A game in which you will feel like you're actual riding. A game in which creativity and beauty are combined. ");
        inString = new StringBuffer(inString.toString().replaceAll(" +", " "));
        }
}

So I try to use System.out.println(main.inString); in my Textcl.class, but get null.

4
Because you have never initialize it in other static void main you running thereYoda

4 Answers

3
votes

You will get null because inString is never initialized as rightly pointed by Robert Kilar in the comment.

You refer to static variables by using the class name.

Example ClassName.variablename. In your case

    main.inString 

Run your main class. When you run inString is initialized in the constructor of the class. Now you can refer to the same in Myclass as below.

public class main {
public static StringBuffer inString;

public main()
{
inString = new StringBuffer("Our aim is to make a 15 realistic game, where grinding powerlines and doing a tailwhip isn't easy, like in the real world. A game in which you will feel like you're actual riding. A game in which creativity and beauty are combined. ");
inString = new StringBuffer(inString.toString().replaceAll(" +", " "));
new MyClass();
}
public static void main(String[] args) {
    new main();
    }
}

Now in MyClass refer to the static variable.

class MyClass {

public MyClass() {
    System.out.println("............."+main.inString);// refer to static variable
}
}

You can also pass variables to the constructor of a class.

public class main {
public  StringBuffer inString;

 public main()
  {
    inString = new StringBuffer("Our aim is to make a 15 realistic game, where grinding powerlines and doing a tailwhip isn't easy, like in the real world. A game in which you will feel like you're actual riding. A game in which creativity and beauty are combined. ");
    inString = new StringBuffer(inString.toString().replaceAll(" +", " "));  
    new MyClass(inString);
 }
public static void main(String[] args) {
    new main();

    }
}

Then in Myclass

 public class MyClass
 {
        public MyClass(StringBuffer value)
        {
          System.out.println("............."+value);
        }
 } 

Please check the link @ Why are static variables considered evil?

4
votes

You can get to it by: main.inString where main is the name of the class where public static variable is declared.

2
votes

Since you made the field in the class static, you use the class name to access it, i.e.

main.inString
1
votes

Use JavaBeans and store in it as one of its fields and use getters and setter for that.

JavaBeans are Java classes that have properties. Think of properties as private instance variables. Since they're private, the only way they can be accessed from outside of their class is through methods in the class. The methods that change a property's value are called setter methods, and the methods that retrieve a property's value are called getter methods.

public class VariableStorage implements Serializable  {

    private String inString;

    public String getInString() {
        return inString;
    }

    public void setInString(String inString) {
        this.inString = inString;
    }
}

Set the variable in your mail method by using:

VariableStorage variableStorage = new VariableStorage();
variableStorage.setInString(inString);

Then use object serialzation to serialize this object and in your other class deserialize this object.

In serialization an object can be represented as a sequence of bytes that includes the object's data as well as information about the object's type and the types of data stored in the object.

After a serialized object has been written into a file, it can be read from the file and deserialized. That is, the type information and bytes that represent the object and its data can be used to recreate the object in memory.

If you want a tutorial for this, refer to Serialization in Java.