Results 1 to 8 of 8
- 06-22-2011, 07:16 AM #1
Senior Member
- Join Date
- Mar 2009
- Posts
- 126
- Rep Power
- 0
help me please in "Access control " :(
hi guys ,
Actually i just want to know what really happening behind the screen .
1. why the private member of the class is restricted to its own object when it is created in another class ?
2. how this is happening behind the screen ?
please check the below code and explain me guys
3. And another silly doubt , if class is just logical unit then how the public static void main() method alone is executed without creating object ?Java Code:class Test { int a; // default access public int b; // public access private int c; // private access // methods to access c void setc(int i) { // set c's value c = i; } int getc() { // get c's value return c; } } class AccessTest { public static void main(String args[]) { Test ob = new Test(); // These are OK, a and b may be accessed directly ob.a = 10; ob.b = 20; // This is not OK and will cause an error // ob.c = 100; // Error! // You must access c through its methods ob.setc(100); // OK System.out.println("a, b, and c: " + ob.a + " " + ob.b + " " + ob.getc()); } }
- 06-22-2011, 07:28 AM #2
I don't understand what you are asking.
I don't understand what you are asking. What do you not understand about the comments in the code. By the way you mean "scene" not "screen"2. how this is happening behind the screen ?
Your program is run from the JVM. Since it is static it does not need to create an object. It would execute your program by calling YourProgram.main(args);3. And another silly doubt , if class is just logical unit then how the public static void main() method alone is executed without creating object ?
- 06-22-2011, 07:40 AM #3
Senior Member
- Join Date
- Mar 2009
- Posts
- 126
- Rep Power
- 0
Sorry Junky for my lack of explanation ,
in the below program int c is declared as private in class Test , since it is private it can be accessed only by the class Test right ? but object is just a physical unit of class , so if i create a object of class Test then that object must have rights to access the private member int c directly know , then why it is restricted to access directly !
i mean this line -->" ob.c = 100" in the below program :( . it may be very silly doubt , but i want to know what really happening in JVM i.e., behind scene
Java Code:class Test { int a; // default access public int b; // public access private int c; // private access // methods to access c void setc(int i) { // set c's value c = i; } int getc() { // get c's value return c; } } class AccessTest { public static void main(String args[]) { Test ob = new Test(); // These are OK, a and b may be accessed directly ob.a = 10; ob.b = 20; // This is not OK and will cause an error // ob.c = 100; // Error! // You must access c through its methods ob.setc(100); // OK System.out.println("a, b, and c: " + ob.a + " " + ob.b + " " + ob.getc()); } }Last edited by funkygarzon; 06-22-2011 at 07:45 AM.
- 06-22-2011, 07:51 AM #4
Yes an object of the class can access the variable but in the program it is not the object that is trying to access it. The main method in a totally different class is trying to access it.
- 06-22-2011, 08:01 AM #5
Senior Member
- Join Date
- Apr 2010
- Location
- Philippines
- Posts
- 580
- Rep Power
- 4
No, because it is private though you created class Test in class AccessTest.
Are you asking why you have to define the access level of an object?
See Controlling Access to Members of a Class (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
At the bottom of the page you'll see Tips on Choosing an Access Level
- 06-22-2011, 08:28 AM #6
Senior Member
- Join Date
- Mar 2009
- Posts
- 126
- Rep Power
- 0
- 06-22-2011, 08:37 AM #7
I'm not sure what you are trying to say here.
In the AccessTest class you create an object of Test class. In the Test class a variable has a private access modifier. This means that an object of the AccessTest class (or any other class) cannot have direct access to that variable. It has to ask permission to do so and this is achieved via an accessor method.
- 06-22-2011, 09:48 AM #8
Senior Member
- Join Date
- Mar 2009
- Posts
- 126
- Rep Power
- 0
Similar Threads
-
"Access is denied" error while file creation
By Bharath_M in forum Advanced JavaReplies: 4Last Post: 04-10-2011, 12:23 PM -
connection = DriverManager.getConnection(DATABASE_URL,'"+userid +"','"+password+"');
By renu in forum New To JavaReplies: 3Last Post: 10-12-2010, 04:21 PM -
How to change my form design from "metal" to "nimbus" in Netbeans 6.7.1?
By mlibot in forum New To JavaReplies: 1Last Post: 01-21-2010, 09:20 AM -
MoneyOut.println("It took you (whats wrong?>",year,"<WW?) years to repay the loan")
By soc86 in forum New To JavaReplies: 2Last Post: 01-24-2009, 06:56 PM -
the dollar sign "$", prints like any other normal char in java like "a" or "*" ?
By lse123 in forum New To JavaReplies: 1Last Post: 10-20-2008, 07:35 AM


LinkBack URL
About LinkBacks
Reply With Quote
. Now i clearly understood buddy :)

Bookmarks