Results 1 to 8 of 8
  1. #1
    funkygarzon is offline Senior Member
    Join Date
    Mar 2009
    Posts
    126
    Rep Power
    0

    Default 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

    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());
    }
    }
    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 ?

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Quote Originally Posted by funkygarzon View Post
    1. why the private member of the class is restricted to its own object when it is created in another class ?
    I don't understand what you are asking.

    2. how this is happening behind the screen ?
    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"

    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 ?
    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. #3
    funkygarzon is offline Senior Member
    Join Date
    Mar 2009
    Posts
    126
    Rep Power
    0

    Default

    Quote Originally Posted by Junky View Post
    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"



    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);
    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 08:45 AM.

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    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.

  5. #5
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    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

  6. #6
    funkygarzon is offline Senior Member
    Join Date
    Mar 2009
    Posts
    126
    Rep Power
    0

    Default

    Quote Originally Posted by Junky View Post
    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.

    Quote Originally Posted by mine0926 View Post
    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
    thank you very much for your wonderful help Junky and mine0926 ,

    So ,behind the scene ,JVM will consider the class Test object as class AccessTest 's object since it is created inside class AccessTest right ?

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Quote Originally Posted by funkygarzon View Post
    JVM will consider the class Test object as class AccessTest 's object since it is created inside class AccessTest right ?
    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.

  8. #8
    funkygarzon is offline Senior Member
    Join Date
    Mar 2009
    Posts
    126
    Rep Power
    0

    Default

    Quote Originally Posted by Junky View Post
    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.

    Oh thank you very much Junky,

    This is what i am trying to explain you , actually i am weak in English , sorry . Now i clearly understood buddy :)

Similar Threads

  1. "Access is denied" error while file creation
    By Bharath_M in forum Advanced Java
    Replies: 4
    Last Post: 04-10-2011, 01:23 PM
  2. Replies: 3
    Last Post: 10-12-2010, 05:21 PM
  3. Replies: 1
    Last Post: 01-21-2010, 10:20 AM
  4. Replies: 2
    Last Post: 01-24-2009, 07:56 PM
  5. Replies: 1
    Last Post: 10-20-2008, 08:35 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
  •