Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By pbrockway2

Thread: How do you access private instance variables from another class?

  1. #1
    javaa is offline Member
    Join Date
    Aug 2012
    Posts
    28
    Rep Power
    0

    Default How do you access private instance variables from another class?

    Hi guys! I have this assignment and I need to have private instance variables but I also need to access them!

    Here is the first part.

    Java Code:
    public class ConstructorDemo1 {
    	public static void main( String [] args ) {
    		Cube C1 = new Cube();
    
    		System.out.println();
    		System.out.println( "side is " + c1.getSide() );
    	}
    }
    here is the code that has the stuff Im supposed to access:
    Java Code:
    public class Cube {
    	private double side;
    
    	public void setSide(double s) {
    		if (s > 0)
    			side = s;
    		else
    			error_message();
    	}
    
    	public double calculateVolume() {
    		return side * side * side;
    	}
    
    	private void error_message() {
    		System.out.println( "ERROR: Sides must be > 0");
    	}
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: How do you access private instance variables from another class?

    You use getter and setter methods, also known as accessor and modifier methods.

    You already appear to be trying to call a getter method, getSide(), but don't appear to have given your Cube class this method yet. I suggest that you do this. Also take care of your capitalization as c1 is not the same as C1. If your code produces any errors you need to post those errors here so we can better understand any problems you might be having.

  3. #3
    javaa is offline Member
    Join Date
    Aug 2012
    Posts
    28
    Rep Power
    0

    Default Re: How do you access private instance variables from another class?

    Thank you so much!
    I updated my programs so they are now:

    Java Code:
    public class ConstructorDemo1 {
    	public static void main( String [] args ) {
    		Cube c1 = new Cube();
    
    		System.out.println();
    		System.out.println( "side is " + c1.side);
    	}
    }
    and

    Java Code:
    public class Cube {
    	private double side;
    
    	public void setSide(double s) {
    		if (s > 0)
    			side = s;
    		else
    			error_message();
    	}
    
    	public double calculateVolume() {
    		return side * side * side;
    	}
    
    	private void error_message() {
    		System.out.println( "ERROR: Sides must be > 0");
    	}
    }

    The error I am getting is:
    ConstructorDemo1.java:6: error: side has private access in Cube
    System.out.println("side is " + c1.side);
    ^
    1 error

    I know I can fix this by making side public but one of the requirements is that side is private.

    Is there a way to call private variables from another class??
    Last edited by javaa; 08-26-2012 at 01:40 AM.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: How do you access private instance variables from another class?

    Is there a way to call private variables from another class?
    Variables are made private precisely so that you don't access them from other classes. So don't do that.

    In #2 Fubarable suggested you create a public getter method getSide(). Something like this:

    Java Code:
    public class Cube {
        // other stuff here as you already have
    
        public double getSide() {
            // your code here
        }
    }
    Inside main() you don't try and access the private variable, instead you call the public method.
    Fubarable likes this.

  5. #5
    javaa is offline Member
    Join Date
    Aug 2012
    Posts
    28
    Rep Power
    0

    Default Re: How do you access private instance variables from another class?

    oh ok! thank you so much!

Similar Threads

  1. Replies: 2
    Last Post: 06-03-2011, 10:15 PM
  2. Why make class variables private?
    By PrinceSendai in forum New To Java
    Replies: 3
    Last Post: 10-18-2010, 12:01 PM
  3. Access Public Global Variables in class
    By spatel14 in forum New To Java
    Replies: 5
    Last Post: 07-08-2010, 11:50 AM
  4. Access Public Global Variables in class
    By spatel14 in forum New To Java
    Replies: 1
    Last Post: 07-07-2010, 08:41 PM
  5. Replies: 3
    Last Post: 03-14-2009, 08:22 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •