Why can I access a private variable from main meth

2020-02-01 02:03发布

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

7条回答
仙女界的扛把子
2楼-- · 2020-02-01 02:37

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

查看更多
登录 后发表回答