Results 1 to 5 of 5
- 08-25-2012, 11:40 PM #1
Member
- Join Date
- Aug 2012
- Posts
- 28
- Rep Power
- 0
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.
here is the code that has the stuff Im supposed to access: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() ); } }
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"); } }
-
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.
- 08-26-2012, 12:27 AM #3
Member
- Join Date
- Aug 2012
- Posts
- 28
- Rep Power
- 0
Re: How do you access private instance variables from another class?
Thank you so much!
I updated my programs so they are now:
andJava Code:public class ConstructorDemo1 { public static void main( String [] args ) { Cube c1 = new Cube(); System.out.println(); System.out.println( "side is " + c1.side); } }
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 12:40 AM.
- 08-26-2012, 12:55 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Re: How do you access private instance variables from another class?
Variables are made private precisely so that you don't access them from other classes. So don't do that.Is there a way to call private variables from another class?
In #2 Fubarable suggested you create a public getter method getSide(). Something like this:
Inside main() you don't try and access the private variable, instead you call the public method.Java Code:public class Cube { // other stuff here as you already have public double getSide() { // your code here } }
- 08-26-2012, 01:09 AM #5
Member
- Join Date
- Aug 2012
- Posts
- 28
- Rep Power
- 0
Similar Threads
-
Can you use the super keyword when variables in parent class are private?
By lam5442 in forum New To JavaReplies: 2Last Post: 06-03-2011, 09:15 PM -
Why make class variables private?
By PrinceSendai in forum New To JavaReplies: 3Last Post: 10-18-2010, 11:01 AM -
Access Public Global Variables in class
By spatel14 in forum New To JavaReplies: 5Last Post: 07-08-2010, 10:50 AM -
Access Public Global Variables in class
By spatel14 in forum New To JavaReplies: 1Last Post: 07-07-2010, 07:41 PM -
Private or Protected access for super class variables
By Madushan in forum New To JavaReplies: 3Last Post: 03-14-2009, 07:22 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks