【问题标题】:What will happen when we create an object outside the method?当我们在方法之外创建对象时会发生什么?
【发布时间】:2014-10-09 16:04:24
【问题描述】:

我有一个与 OOP 相关的问题。

如果我们在方法之外创建一个对象会有什么效果。

那个对象会变成全局对象吗?

这是一些代码..

class A(){
 String b;
 public void c(){
  //some code in here}
 }

 class C(){

 A a = new A(); //is this ok and if it is ok what is this mean??

 public void p(){
 a.c(); //Is this possible??
}
}

【问题讨论】:

  • 对象只能在方法中实例化。所以一开始是不可能的。
  • 如果它是静态的,它将是全局的。否则它将是一个实例成员,可用于该类的所有非静态方法(也可能用于其他类,具体取决于其访问修饰符)。
  • 您不能在任何方法之外创建对象。并且对象不是本地的、全局的、实例范围——只有它们的 references 具有这样的属性。
  • 是的。如果变量在类内部但在任何方法之外声明,则该变量是“全局的”

标签: java oop object


【解决方案1】:

您似乎对 Java 或面向对象编程很陌生。

在Java中创建对象有两个步骤:声明和初始化

声明表示您正在声明某事存在。 例如:String title; 表示,存在一个名为 title 的字符串对象。
Initialization 正在为其赋值。即:title = "Hello World!";。但是在初始化一个对象之前,您需要确保它已被声明。你也可以在一个语句中结合声明和初始化:String title = "Hello World!";

对象的范围取决于您声明该对象的位置。 如果你在这样的类中声明它:

class Car {
  String name; //These are called fields
  static String author = "Farseen"
  public void doSomething() {..../*can access name and author*/}
  public static void doSomething() {..../*can access author only*/}
}

类中的所有东西都可以访问它,除了静态(我们稍后会讲到)方法。这些被称为字段
如果你在一个方法中声明它,只有那个方法可以访问它:

class Car {
  public void doSomething() {
    String name = "BMW"; //These are called local variables
  }
  public void doSomeOtherThing() {
    //Cannot acccess name
  }
}

这些被称为局部变量

如果你在一个类之外声明它,抱歉 Java 不允许你这样做。一切都必须在一个类中。

您可以在方法之外为声明添加前缀,即带有访问修饰符的字段:

public :使任何人都可以访问该字段。合法:
Car myCar = new Car();System.out.println(myCar.name);

private :使该字段只能被类内的方法(函数)访问。 非法到 :
Car myCar = new Car();System.out.println(myCar.name);

protected :使该类的子类可以访问该字段。用户也无法访问它,例如私有。

现在加入 static 修饰符:

它表示字段或方法(函数)属于汽车的整个物种,而不是单个汽车。
像这样访问静态字段author 是合法的:Car.author 无需创建单独的汽车,虽然它是合法的:new Car().author
静态的东西只知道静态的东西,不知道个别的东西。

Java 中没有全局变量的概念。虽然,您可以使用以下方式实现它:

class Globals {
    public static String GLOBAL_VARIABLE_MESSAGE = "Hello World!";
}

并在代码中的某处使用Globals.GLOBAL_VARIABLE_MESSAGE

希望对你有帮助!

编辑:参考添加到问题中的代码

class A{ //Parenthesis are NOT needed
         // or allowed in class definition:
         // 'class A()' is wrong
 String b;
 public void c(){
  //some code in here
 }
}

 class C{ //Same here. No parenthesis needed
  A a = new A(); //Q: Is this ok and if it is ok what is this mean??
                 //A: Yes, completely ok.
                 // This means you added
                 // a field 'a' of type A to the class C

 public void p(){
  a.c(); //Q: Is this possible??
         //A: Of course
 }
}

【讨论】:

    【解决方案2】:

    我对您的代码进行了一些编辑并为您的疑问添加了答案

    class A{ 
     String b;
     public void c(){
      //some code in here}
     }
    
     class C{
    
     A a = new A(); //is this ok and if it is ok what is this mean??--It is Ok.it means that a is instance variable of type A.
    
     public void p(){
     a.c(); //Is this possible?? -- It is possible. since c() is a method in class A.
    }
    }
    

    【讨论】:

      【解决方案3】:

      创建对象的位置与它是“全局的”几乎没有关系。这完全是关于该对象的 reference 的可访问范围。给出两个极端的对立面,都涉及方法之外的实例化:

      public static final File f = new File(new File("/x/a"), "b");
      

      上述行将导致File 的两个实例被创建:一个将变为全局,另一个将完全不可访问并当场处理。

      【讨论】:

      • 实际上,这不在方法之外。代码在静态初始化方法(<clinit>)中执行。
      • 好的,技术上。但我不认为这就是 OP 所说的“方法”。
      猜你喜欢
      • 1970-01-01
      • 2014-03-02
      • 1970-01-01
      • 2013-06-21
      • 2015-09-16
      • 2023-04-08
      • 1970-01-01
      • 2013-10-13
      • 2018-12-28
      相关资源
      最近更新 更多