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: Is it possible to reference Variables from another class

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Is it possible to reference Variables from another class

    Whats the best way to reference variables from another class in a project, I know how to do it to objects but not variables.

    Thanks


  2. #2
    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: Is it possible to reference Variables from another class

    Add getter and setter methods to the class containing the variable and use them to get to the variable.
    Or you could make the variable public.

  3. #3
    Junior Member
    Join Date
    Jul 2011
    Posts
    28
    My Mood
    Busy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Is it possible to reference Variables from another class

    Use static variables. They are class variables.You will always get updated values when they are altered between methods of different classes.

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Is it possible to reference Variables from another class

    Quote Originally Posted by rohan22 View Post
    Use static variables. They are class variables.You will always get updated values when they are altered between methods of different classes.
    This is very context dependent and often NOT the way to go about things. Norm's recommended method would be the default way to go unless you want the value of a variable to be the same across all classes.

  5. #5
    Member
    Join Date
    Feb 2012
    Posts
    106
    My Mood
    Yeehaw
    Thanks
    8
    Thanked 11 Times in 11 Posts

    Default Re: Is it possible to reference Variables from another class

    Quote Originally Posted by JoeBrown View Post
    Whats the best way to reference variables from another class in a project, I know how to do it to objects but not variables.

    Thanks
    Class variables are normally meant to be kept safe for a reason.
    When you have an exception to the rule, (only you can prevent bad code) you have options.

    Getter methods are lame, but simple and to the point, and they keep most of your protection intact.

    If your variable(number) is specific to an Object
    public class Creator {
    	private int number; // I am unique to every Creator Object
     
    	public Creator(){
    		number = 7;
    	}
     
    	public int getNumber(){ return number;}
     
    	//main method
    	public static void main(String[] args){
    		int giveMeNumber;
    		Creator user = new Creator();
     
    		giveMeNumber = user.getNumber(); // returns the number variable
    	}
    }

    If your varaible(number) is used by all Objects in the Class
    public class Creator {
    	private static int number = 7; //I exist once for all Creator Object to share me. (Should be initialized almost ALWAYS)
     
    	public static int getNumber(){ return number;} //NOTICE** the method is now static
     
    	//main method
    	public static void main(String[] args){
    		int giveMeNumber;
     
    		giveMeNumber = Creator.getNumber(); // the Class itself is used to gain permission.
     
    		//Though, this will still work and is useful in situations. (AKA: nothing wrong with it)
    		Creator user = new Creator();
    		giveMeNumber = user.getNumber(); // returns the number variable
    	}
    }

    hope this helps!
    Jonathan

Similar Threads

  1. Instance Variables and local variables difference
    By dcwang3 in forum Java Theory & Questions
    Replies: 3
    Last Post: October 31st, 2011, 06:33 AM
  2. [SOLVED] Instance data member vs Local variables (primitive/object reference)
    By chronoz13 in forum Java Theory & Questions
    Replies: 1
    Last Post: September 23rd, 2011, 12:42 AM
  3. Object Reference
    By Mr.777 in forum Object Oriented Programming
    Replies: 2
    Last Post: June 13th, 2011, 03:03 AM
  4. Calling the actionlistener class depending on variables
    By zidsal in forum Java Theory & Questions
    Replies: 3
    Last Post: January 5th, 2011, 08:52 AM
  5. Object as a Reference into Object's Class
    By Ace Coder in forum Object Oriented Programming
    Replies: 6
    Last Post: November 30th, 2010, 12:22 PM