Why can I access a private variable from main method?
Asked Answered
H

8

12
package com.valami;

 public class Ferrari
 {
  private int v = 0;


  private void alam()
  {
   System.out.println("alam");
  }

  public Ferrari()
  {
   System.out.println(v);
  }



  public static void main(String[] args)
  {
   Ferrari f = new Ferrari();
   f.v = 5;
   System.out.println(f.v);
  }

 }

Hi all! I have one simple question.... WHY can I reach a private variable from the main method ? I know, I'm in the containing class, but it is main. I believed the main is NOT part of the class which is containing it... Then I would not to reach an private member, but I can....WHY? Please help...thx

Hyps answered 16/1, 2011 at 19:21 Comment(0)
D
5

Main is a part of you class, you have declared it inside your class :) What main is not is part of your object, it will not be any part of the objects you create from the class but it is still part of the class. This is correct for any static function as main is just a normal static function that the framework knows it should look for when the program is executed.

Deflate answered 16/1, 2011 at 19:25 Comment(0)
S
11

Classes can access the private instance variables of (other) objects of the same type.

The following is also possible

public class Foo {

    private int a;

    public void mutateOtherInstance(Foo otherFoo) {
        otherFoo.a = 1;
    }
}

You could argue if this is desirably or not, but it's just a rule of life that the JLS has specified this is legal.

Soupandfish answered 16/1, 2011 at 19:31 Comment(7)
Up-voted - I never knew this was possible. I'm a C# guy rather than Java but I have just tested this in C# and it works the same way.Laxative
It's also possible in C# and I think C++ (but don't quote me on that), perfect for internal sort and equals functions when you have the current object (this) and a instance of the same class and need to compare private data.Deflate
Interesting, I never knew this about C#. In C++ this is definitely legal as well.Soupandfish
@ArjanTijms I want to read up more on this. Could you help me find a link? I am not sure what I should be searching for.Elocution
@Elocution From the top of my head, this book at least explains it: amazon.com/dp/0071591060 You might want to look into the JSL to see if it contains some more details.Soupandfish
@ArjanTijms Thanks I will take a look at it. Btw, is there a convention, whether to directly access or use getters?Elocution
@Elocution Within your own class you typically directly access, while for another object you nearly always use getters. The other object being an instance of the same class is a special case really.Soupandfish
D
5

Main is a part of you class, you have declared it inside your class :) What main is not is part of your object, it will not be any part of the objects you create from the class but it is still part of the class. This is correct for any static function as main is just a normal static function that the framework knows it should look for when the program is executed.

Deflate answered 16/1, 2011 at 19:25 Comment(0)
H
3

The main method is in the class Ferrari and thus can access private variables, even if it's static.

Houseboy answered 16/1, 2011 at 19:24 Comment(0)
T
1

Well, main() is part of the containing class. In fact, main() is exactly like every other method, except that you can start the JVM and tell it to run the main() method of a class from the command line.

Tishatishri answered 16/1, 2011 at 19:24 Comment(0)
C
1

As long as the private variable is in the same class as the main() method, then the main() method has access to it. In general, even static methods have access to private fields of instances of the same class.

Chalybite answered 16/1, 2011 at 19:25 Comment(0)
L
1

The only special feature of the main method is it is used to tell the compiler where program execution should begin. Other than that it behaves just like any other class method and has access to private variables like any other class method.

Laxative answered 16/1, 2011 at 19:26 Comment(0)
F
0

Since you have made the main method in the same class as the private attribute v, you can access it. Privates attribute or methods are accessible to its own class.

That is to say, if you moved the main method to another class, you cannot access anything that has a private modifier.

Franci answered 13/8, 2023 at 4:27 Comment(0)
R
-2

Because main is static and your class hasn't been instantiated.

e.g., you have no Ferrari object to access. You must create a Ferrari object then access it's members. static main is a special static function. You can think of it as sort of separate if you want. So if you moved your main method outside of Ferrari you would expect that you would have to create an instance of Ferrari to use it... same deal here.

Repentance answered 16/1, 2011 at 19:27 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.