Results 1 to 8 of 8
  1. #1
    maheshpwr is offline Member
    Join Date
    Mar 2011
    Posts
    5
    Rep Power
    0

    Smile Access Specifiers

    class A
    {
    private int a;
    }


    class main
    {
    public static void main(String a[])
    {
    A m=new A();

    m.a=10 /// can we write this statment????

    }



    // And what if a is public and protected?????????????????
    }

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    You can not directly access private variables. As for protected and public I will let you test that, switch a to protected then try modifying it directly in main, then compile and run it and see what happens. Try the same thing with public.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,236
    Rep Power
    19

    Default

    And/or read all about access and declaration modifiers:
    Names

    db

  4. #4
    veeraswamy is offline Member
    Join Date
    Mar 2011
    Posts
    1
    Rep Power
    0

    Default U cant

    since the data member is private it uses to access only within the methods of that class only,though the object of that class is created in another class.

  5. #5
    santoshpai1 is offline Member
    Join Date
    Mar 2011
    Location
    Bangalore, India
    Posts
    5
    Rep Power
    0

    Default

    Private members of a class cannot be accessed by anybody.
    Next level of abstraction is Protected. This means, all the classes derived from the current class can access thr variable as public.
    The last level is Public. Anybody can access public members.

  6. #6
    maheshpwr is offline Member
    Join Date
    Mar 2011
    Posts
    5
    Rep Power
    0

    Default

    thanx.
    for all answers.
    But Can u tell with an example?????

  7. #7
    maheshpwr is offline Member
    Join Date
    Mar 2011
    Posts
    5
    Rep Power
    0

    Default

    thanx.
    for all answers.
    But Can u tell with an example?????

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,520
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by maheshpwr View Post
    thanx.
    for all answers.
    But Can u tell with an example?????
    Your original example is a bit misleading because your main( ... ) method is defined in that class itself so it has all access rights to any member, even if it is private. For an experiment: define your main( ... ) method in another class and see for yourself.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 0
    Last Post: 01-10-2011, 06:26 AM
  2. Format Specifiers
    By Cass29 in forum New To Java
    Replies: 3
    Last Post: 12-20-2009, 03:49 PM
  3. Default Access (package access) confusion
    By gauravrajbehl in forum New To Java
    Replies: 1
    Last Post: 11-18-2009, 10:48 AM
  4. Method access or field access
    By carderne in forum New To Java
    Replies: 2
    Last Post: 12-06-2008, 06:20 PM
  5. Replies: 1
    Last Post: 08-07-2007, 07:51 AM

Tags for this Thread

Posting Permissions

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