prosseek
prosseek

Reputation: 190669

What's the usage of static field in Java?

From the question What's the meaning of System.out.println in Java? I found that out in System.out.println is a static field.

From C/C++ background, it's easy to understand static method, as it's the same as function in C. However, I'm not sure the use case of static field.

Is it just a way to use multiple methods without instantiating an object just as we use System.out.println without instantiating anything? Or is there any use cases for static field?

Upvotes: 0

Views: 1220

Answers (7)

scott
scott

Reputation: 974

In addition to the .out var in System, it can either be used as a shared variable that all instances of a class can update

private static int meatballsConsumed;

Or as a general-purpose shared variable

public static String thisSeemsDangerous;

Or as a constant

public static final String FLD_OF_DREAMS = "COSTNER,KEVIN";

Upvotes: 0

Achrome
Achrome

Reputation: 7821

static variables/methods not only have the property of being used without instantiation, but they are also consistent across multiple instances.

For example,

public class A {
    public int a = 1;
    public static int b = 2;
}

Now, when I do A a1 = new A() and A a2 = new A(), A.a gets 2x the memory and is stored in the object instance, while A.b gets the memory only once and is stored outside the instance.

A prime example of this would be

a1.b = 3;
System.out.println(a2.b);

This will print 3, instead of 2, because a1 changed the value of b for the whole class, and therefore, all the instances.

Upvotes: 3

Dancrumb
Dancrumb

Reputation: 27529

Static fields are also known as 'class' fields (as opposed to 'instance' fields).

That means that they are accessible without you needing to instantiate the class first.

So, you can call class methods (like Math.abs() on the Math class) without having to instantiate a Math class. You can also access properties like Math.PI.

Also, changing a class property means that it affects all instances of that class, meaning that every object that was instantiated will see this value change, allowing you to affect them with a single property change.

Upvotes: 0

Aniket Inge
Aniket Inge

Reputation: 25695

out is an object of PrintStream.

System is a class in java.lang package

println is an instance method(not a static method) of PrintStream class

To access the field out in System without instantiating System, the field is declared static.

Upvotes: 1

A static field, is a field that is set-up and can be get without instantiating a class (using new ClassName()).

For example:

public class MyClass {
  public static int number = 1;
}

With the code above, you can get the "number" field using MyClass.number.

public class MyClass {
  public int number = 1;
}

Now, you need to instantiate MyClass via constructor. Since there is no constructor declared, you just use new MyClass():

MyClass cl = new MyClass();
cl.number; // <-- The number

Upvotes: 0

austin
austin

Reputation: 5866

The System class has only one instance of the OutputStream that writes to standard output (called out) so it's a static variable. We don't need more than one instance because there's only one standard output.

Upvotes: 0

kufudo
kufudo

Reputation: 2833

A static field is a property of the class, which gets allocated on the heap and is independent of a particular object instance.

You could use a static variable to count the number of instances of a class for example.

Upvotes: 3

Related Questions