开发者

java objects, shared variables

开发者 https://www.devze.com 2022-12-29 12:51 出处:网络
I have a simple question here. If I declare a variable inside an obje开发者_如何学运维ct which was made [declared] in the main class, like this:

I have a simple question here. If I declare a variable inside an obje开发者_如何学运维ct which was made [declared] in the main class, like this:

public static int number;

( usually I declare it like this :

private int number;

)

can it be used in a different object which was also made [declared] in the main class? btw I do not care about security atm, I just want to make something work, don't care about protection)


Here's a telling quote from Java Language Specification:

JLS 8.3.1.1 static Fields

If a field is declared static, there exists exactly one incarnation of the field, no matter how many instances (possibly zero) of the class may eventually be created. A static field, sometimes called a class variable, is incarnated when the class is initialized.

A field that is not declared static (sometimes called a non-static field) is called an instance variable. Whenever a new instance of a class is created, a new variable associated with that instance is created for every instance variable declared in that class or any of its superclasses.

[Example program follows...]

In short, a static field is a class variable: it belongs to the class, as opposed to the instances of the class. In a way, you can think of a static field as a variable shared by instances of the class, but it's much more consistent to think of static fields as belonging to the class, just like static method also belongs to the class, etc.

Since they belong to the class, they do not require an instance of said class to access (assuming adequate visibility), and in fact it's considered bad programming practice to access static members through an instance instead of a type expression.

Related questions

  • Java - static methods best practices
  • Static methods
  • calling non-static method in static method in Java
  • non-static variable cannot be referenced from a static context (java)
  • When NOT to use the static keyword in Java?
  • Static variables and methods


If the class holding 'number' is called MyClass you can refer to it as MyClass.number from any method.

Doing so for a variable is not good design though.


There are really two issues here: public vs. private in the context of inner classes, and static variables.

Part 1:

static means that you don't need an instance of the class to access that variable. Suppose you have some code like:

class MyClass {
 public static String message = "Hello, World!";
}

You can access the property this way:

System.out.println(MyClass.message);

If you remove the static keyword, you would instead do:

System.out.println(new MyClass().message);

You are accessing the property in the context of an instance, which is a copy of the class created by the new keyword.

Part 2:

If you define two classes in the same java file, one of them must be an inner class. An inner class can have a static keyword, just like a property. If static, it can be used separately. If not-static, it can only be used in the context of a class instance.

Ex:

class MyClass {
 public static class InnerClass {
 }
}

Then you can do:

new MyClass.InnerClass();

Without the 'static', you would need:

new MyClass().new InnerClass(); //I think

If an inner class is static, it can only access static properties from the outer class. If the inner class is non-static, it can access any property. An inner class doesn't respect the rules of public, protected, or private. So the following is legal:

class MyClass {
 private String message;

 private class InnerClass {
  public InnerClass() {
   System.out.println(message);
  }
 }
}

If the inner class has keyword static, this would not work, since message is not static.


static variables are shared by all instances of a given class. If it's public, it is visible to everything. non-static variables belong to only one instance.

Since your main method is static, it can only see static variables. But you should avoid working statically - make an instance of a class, and pass the data around as method/constructor parameters, rather than sharing it via static variables.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号