Reputation: 41
I've recently started learning Java. If private variables can be directly accessed by objects in main()
how are they "private"?
public class Account1 {
private int accountNum;
private String name;
Account1() {
accountNum = 1101;
name = "Scott";
}
public void showData() {
System.out.println("Account Number: " + accountNum +
"\nName: " + name);
}
public static void main(String[] args) {
Account1 myA1 = new Account1();
myA1.showData();
System.out.println(myA1.accountNum); //Works! What about "Private"?!
}
}
This gives the output:
Account Number: 1101
Name: Scott
1101
Upvotes: 4
Views: 4408
Reputation: 338614
private
Fields, Methods, and Constructors accessible from their enclosing top-level classThe Java Language Specification is quite clear and explicit on this topic. See Example 6.6-5. Access to private Fields, Methods, and Constructors for text and for example code.
To quote:
A
private
class member or constructor is accessible only within the body of the top level class (§7.6) that encloses the declaration of the member or constructor. It is not inherited by subclasses.
private
member fieldsYou put your main
method within the class containing the private
member fields. Therefore code within the main
can access those private
member fields. If you copy that very same main
method to another class, it will not compile.
This code compiles successfully:
package work.basil.example.linguistics;
public class Point
{
private int x = 42;
public static void main ( String[] args )
{
Point p = new Point ( );
System.out.println ( "p.x = " + p.x ); // Compiles successfully, because private member field is contained within the same class.
}
}
Try copy-pasting that main
method to another class. That code will fail to compile:
package work.basil.example.linguistics;
public class SomeOtherClass // ⬅️ Different class! Not the `Point` class.
{
public static void main ( String[] args )
{
Point p = new Point ( );
System.out.println ( "p.x = " + p.x ); // Fails to compile, The private member field is *not* contained within this class.
}
}
private
constructorSo too with a private constructor.
A main
method can call new
to instantiate an object via its private
constructor only if within the class owning that constructor. Moving such a main
method to another class will fail to compile.
This code compiles successfully:
package work.basil.example.linguistics;
public class Point
{
private Point ( ) { } // `private` constructor.
public static void main ( String[] args )
{
Point p = new Point ( ); // Compiles successfully, because it is contained within the same class.
System.out.println ( "p = " + p );
}
}
Try copy-pasting that same main
method to some other class. That code fails to compile:
package work.basil.example.linguistics;
public class SomeOtherClass // ⬅️ Different class! Not the `Point` class.
{
public static void main ( String[] args )
{
Point p = new Point ( ); // Fails to compile, because it is outside class with the private constructor.
System.out.println ( "p = " + p );
}
}
Upvotes: 0
Reputation: 6229
Your main is in the Account1 class, so it's still in the same scope.
Private variables can be accessed from any code belonging to the same type. If your main method was in a separate class then it wouldn't be able to access them (without using reflection).
Upvotes: 8
Reputation: 37643
They are private in that they can only be accessed by that class. This means they are accessible from static methods of that class (such as main
) and also from instance methods (such as showData
).
One instance of the class can also access private members of another instance of the class.
If you had a separate class, say, Account2
, it would not be able to access provate members of Account1
.
Upvotes: 0
Reputation: 205
This is because the main() function is a member of the class. It has access to all members of the class.
In real world code, the main function is usually situated in a "harness" class that actually bootstraps the rest of the code. This harness class is usually very lightweight and instantiates other classes that do the real work.
Upvotes: 0
Reputation: 51052
The "main" method of a given class is part of that class. Methods that are part of a class have access to private members of that class. That makes sense to me. Doesn't necessarily mean you should use it, of course.
One way to think about it is to think about one class's knowledge of another class's internal workings. My Person class shouldn't know what happens inside my Order class; it just calls public methods on it. But anything inside Person will of course know about the internal structure of Person -- even a different instance of Person.
Upvotes: 2