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.

Results 1 to 5 of 5

Thread: Post variable in the main class

  1. #1
    Junior Member
    Join Date
    Jan 2014
    Location
    Lithuania
    Posts
    23
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Lightbulb Post variable in the main class

    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


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Post variable in the main class

    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.

  3. #3
    Junior Member
    Join Date
    Jan 2014
    Location
    Lithuania
    Posts
    23
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Post variable in the main class

    I mean Main class which have a public static void main(String[]args) { } method.

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Post variable in the main class

    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

  5. #5
    Junior Member
    Join Date
    Jan 2014
    Location
    Lithuania
    Posts
    23
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Post variable in the main class

    Thanks I fix it THANK THANK!!!!!

Similar Threads

  1. Replies: 18
    Last Post: March 30th, 2013, 09:11 AM
  2. How do I use a variable from main class in another class?
    By El Batman in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 4th, 2013, 07:34 AM
  3. Replies: 2
    Last Post: November 18th, 2012, 02:09 PM
  4. need to make basic class and implementation class (base class without void main)
    By javanewbie101 in forum Object Oriented Programming
    Replies: 1
    Last Post: September 19th, 2012, 08:03 PM
  5. Post variable set to non-null vlaues
    By ss7 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 1st, 2009, 12:04 PM