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 3 of 3

Thread: Need Help Please

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    1
    My Mood
    Busy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post Need Help Please


    Hi everyone!
    I just really need some help...
    could anyone tell me how to get variables from the main java class?
    I'm really having some trouble about that.

    If my question is a bit blurry for you, here is an specific problem:

    Well, I'm creating a game, and I want the total score to be calculated... I declared the variable integer points to main class.. and I'm having trouble getting its value to the other class I created... could anyone help me? thanks..

  2. #2
    Member
    Join Date
    Jan 2013
    Posts
    39
    My Mood
    Relaxed
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Need Help Please

    Its easy, you can make a variable of the main class in the other wher its calculated with

    public static Main main = new Main();

    With this you can use the main for alot of things.
    Including using methodes and variables.
     
    public class Main {
    	public static int x = 5;
     
    	public static void main(String[] args) {
     
    	}
    }
     
    public class second {
     
    	public static Main main = new Main();
    		public second(){
     
     
    		int x;
    		x = main.x;
    	}
    }
    This code it self does nothing but wen the constructor of the second class is called it will get the x of the main class.


    You could initialize it in the 2nd methode too but then the constructor of the main class is called.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need Help Please

    The use of static variables can often cause problems and is not recommended in most cases.
    getting its value to the other class
    For one class to access the contents of an instance of another class, it needs a reference to that instance.
    The preferred technique to access variables is to use getter methods.
    A simple example:
    OtherClass refToOtherClass = new OtherClass();  //  get ref to OtherClass
     
    ...
     
      int myVar = refToOtherClass.getVarValue();
     
    ...
    class OtherClass {
    int var;
    public getVarValue( ) {
       return var;
    }
    ...
    If you don't understand my answer, don't ignore it, ask a question.

  4. The Following User Says Thank You to Norm For This Useful Post:

    curmudgeon (January 25th, 2013)

Tags for this Thread