views:

140

answers:

4

hello, I have a simple question here. If I declare a variable inside an object 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)

A: 

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.

Bozho
To be clear, it can only see static *fields* - variables declared inside the body of a class but not inside a method. That confused me a LOT in the first couple weeks.
Vuntic
Also, different idea so different comment: Working statically is pretty much the same thing as working with global variables and methods. It's the antithesis of OOP, but it's not always a bad thing... yes, Java *is* my primary language.
Vuntic
yes, `fields` = `instance variables`. But since there are no other `static` variables, I think it's ok to call them this way.
Bozho
A: 

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

polygenelubricants
A: 

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.

extraneon
A: 

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.

RMorrisey