Hello guys. I wanna ask you, how to post variable from any class in the main class? I know how to post from main class in the next class, but dunno how to post from next class in the main..
Can you help me?
Thank you
Sorry for bad english language
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
Hello guys. I wanna ask you, how to post variable from any class in the main class? I know how to post from main class in the next class, but dunno how to post from next class in the main..
Can you help me?
Thank you
Sorry for bad english language
Please post an example of what you mean by Main class, Next class, and how you want to share variables between them.
Even when a common written language, like English, may not be the best between us, we should be able to communicate in another language we both know and love, Java.
I mean Main class which have a public static void main(String[]args) { } method.
I built on an earlier example I created for a similar topic. Since you wouldn't give an example, I have no idea if it answers your question, but I've covered most every angle of passing values between classes. If you think of another approach or still have questions, please let me know.
// a class with a main() method to start // a simple class that sends values to another class via the // other class' constructor and using a setter method in the other class public class PassValuesBetweenClassesDemo { // instance variables int rValue; int gValue; int bValue; ReceiveClass receiveClass; // default constructor public PassValuesBetweenClassesDemo() { rValue = 5; gValue = 15; bValue = 25; // create an instance of ReceiveClass and pass the values // through the constructor receiveClass = new ReceiveClass( rValue, gValue, bValue ); // pass an instance of SendClass to ReceiveClass receiveClass.setSendClass( this ); // output the values receiveClass.printRGBValues(); // or, alternatively send the values to the receiveClass // instance using the setter method receiveClass.setRGBValues( rValue + 5, gValue + 5, bValue + 5 ); // output the new values System.out.println( "\nAfter increasing the values by 5, " ); receiveClass.printRGBValues(); // pass the values back from receiveClass to sendClass receiveClass.setValuesInSendClass(); // print the values as now set System.out.println( "\nAfter passing the values back from the" + " receive class,\nthe values in the send class are: " ); System.out.println( "rValue: " + rValue ); System.out.println( "gValue: " + gValue ); System.out.println( "bValue: " + bValue ); } // end default constructor // a main() method public static void main( String[] args ) { new PassValuesBetweenClassesDemo(); } // end method main() // setters to set the values public void setRValue( int rValue ) { this.rValue = rValue; } // end method setRValue() public void setGValue( int gValue ) { this.gValue = gValue; } // end method setGValue() public void setBValue( int bValue ) { this.bValue = bValue; } // end method setBValue() } // end class PassValuesBetweenClassesDemo // a class to receive the values class ReceiveClass { // instance variables private int rValue; private int gValue; private int bValue; private PassValuesBetweenClassesDemo sendClass; // a constructor that accepts the 3 values public ReceiveClass( int rValue, int gValue, int bValue ) { this.rValue = rValue; this.gValue = gValue; this.bValue = bValue; } // end constructor // a method to set the values public void setRGBValues( int rValue, int gValue, int bValue ) { this.rValue = rValue; this.gValue = gValue; this.bValue = bValue; } // end method setRGBValues() // to output the values public void printRGBValues() { System.out.println( "The values in the receive class are: " ); System.out.println( "rValue: " + rValue ); System.out.println( "gValue: " + gValue ); System.out.println( "bValue: " + bValue ); } // end method printRGBValues() // to set the instance variable sendClass public void setSendClass( PassValuesBetweenClassesDemo sendClass ) { this.sendClass = sendClass; } // end method setSendClass() // pass the values back to SendClass public void setValuesInSendClass() { sendClass.setRValue( rValue ); sendClass.setGValue( gValue ); sendClass.setBValue( bValue ); } // end method setValuesInSendClass() } // end class ReceiveClass
Thanks I fix it THANK THANK!!!!!