• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Private Modifier Behaviour

 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class PrivateModifierTest {

private String name = "Test String One";
private int cost = 100;

public static void main(String[] args) {

PrivateModifierTest pmt = new PrivateModifierTest();

System.out.println(pmt.name);
System.out.println(pmt.cost);

Test testClass = new Test();
testClass.test.name = "Test String Two";
testClass.test.cost = 500;

System.out.println(testClass.test.name);
System.out.println(testClass.test.cost);
}
}

class Test{

protected PrivateModifierTest test = new PrivateModifierTest();

/* public static void main(String[] args) {

PrivateModifierTest pmt = new PrivateModifierTest();
System.out.println(pmt.name);
System.out.println(pmt.cost);

Test testClass = new Test();
testClass.test.name = "Test String Two";
testClass.test.cost = 500;

System.out.println(testClass.test.name);
System.out.println(testClass.test.cost);

}*/

}


Now when i compile above program, it compiles well and gives output as :

Test String One
100
Test String Two
500



[Note : Above code is in single file called "PrivateModifierTest.java" ]

Question 1 : Why does the program compile & run successfully. What i was thinking is, it should give error since i am accessing private variables through an instance. Also I am updating the values of private members through an instance of the class without using setter methods ??? Can anyone please explain as to why this thing happens ???

Question 2 : Now when i make Test Class as public and PrivateModifierTest Class as default and move the main() method to Test Class and rename the file to Test.java, Than the same code gives me compilation error, complaining about visibility of private members -- This is just as expected. Thus Question is why it is now giving compilation Error whereas, it was not giving earlier ???


Can anyone please explain as to why is such a behavior for private modifier in detail.

Thanks in Advance.
Saurabh
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the first case, your main method is part of the class whose private members you are trying to access. So it works. In the second case, it obviously won't. Since the main method is a member of the class, private members can be accessed. With regards, as to how it can be accessed through instances, remember that access specifiers work in the context of a class. Instances of the same type can access each others private members, since the access specifiers work in the scope or context of a class.
 
Ranch Hand
Posts: 479
1
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Don't agree with you on this. Access specifiers have relevance in the context of the object? How can two different objects of the same type access each other's private members? Any member that has been declared as private can only be accessed from within the body of the object and not from outside.
 
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Saurabh,

If we talk about the definition of private memembers, it is given below

a)Private members cannot be inherited
b)They can't be accessed outside class, from other classes in same package
or different packages
c)Private members can be accessed from within the same class using its
methods or instance , even without getter and setters( Setters and Mutators are also subjected to Encapsulation or good OOP Design).
In your case.
Scenario 1
-----------
You accessed the private members and set their values within the same
In scenario one,'main' is within scope of the PrivateModifierTest class, so it ran fine satisfying condition 3 above.

Scenario 2
-----------

You moved the main method outside the class scope and private memebers
will act as they don't even exist for other classes in same package or
outisde package, satisfying condition 1 and 2.

Aditionally even if you don't make PrivateModifierTest class default,and
let it remain public, it will give you the same voilation error.

Hope this helps.
Best Regards,
Ben
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When thinking about member access modifiers, its good to think in terms of the code as you see it, rather than in terms of objects/instances. By that I mean;

Members marked private can only be accessed by code in the class in which the private member was declared.

Question 1) It follows therefore that 'private String name' which was declared in class PrivateModiferTest can be accessed by code in that class.

Question 2) When you move the code trying to access the private member to another class, then the same rule applies, so the code doesn't compile.

Hope this helps,

Mike
 
Ben Zaidi
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Raj Kamal:


Don't agree with you on this. Access specifiers have relevance in the context of the object? How can two different objects of the same type access each other's private members? Any member that has been declared as private can only be accessed from within the body of the object and not from outside.



I agree with you, access specifiers do have relevance in context of the
object. Two objects of the same type, if in the same class, can access
each other private variables, as within the class all scope is visible
but they can't access each other members outside the class. So they do
have relevance w.r.t object also.
 
Saurabh Vyas
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank You Benz and Mike and Prakash for your detailed explanation.
It was really helpful.
[ June 18, 2008: Message edited by: Saurabh Vyas ]
 
Something about .... going for a swim. With this tiny ad ...
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic