Java Core

2015/10/25

What does the “static” keyword mean ? Can you override private or static method in Java ?

The static keyword denotes that a member variable or method can be accessed, without requiring an instantiation of the class to which it belongs. A user cannot override static methods in Java, because method overriding is based upon dynamic binding at runtime and static methods binded at compile time.

Fields that have the static modifier in their declaration are called static fields or class variables. They are associated with the class, rather than with any object. Every instance of the class shares a class variables, which is in one fixed location in memory. Any object can change the value of a class variable, but class variables can also be manipulated without creating an instance of the class.

For example, suppose you want to create a number of Bicycle objects and assign each a serial number, beginning with 1 for the first object. This ID number is unique to each object and is therefore an instance variable. At the same time, you need a field to keep track of how many Bicycle objects have been created. Such a field is not related to any individual object, but to the class as a whole. For this you need a class variable, numberOfBicycles.

Static methods, which have the static modifier in their declarations, should be invoked with the class name, without the need for creating an instance of the class.

The static modifier, in combination with the final modifier, is also used to define constants. The final modifier indicates that the value of this field cannot change.

Can you access non-static variable in static context?

No. A static variable in Java belongs to its classes and its value remains the same for all its instances. A static variable is initialized when the class is loaded by the JVM. If your code tries to access a non-static variable, the compiler will complain, because those variables are not created yet.

What is function overriding and overloading in Java?

  • Method overloading in Java occurs when two or more methods in the same class have the exact same name, but different parameters.

  • Method overriding is defined as the case when a child class redefines the same method as a parent class.

What is the difference between an Integer and int in Java?

Integer is a proper object. It even extends from the Number interface, so it can be interchanged with other number alike object types.

But then for performance reasons there are also access to the core primitive types, boolean, int, long, so they can be used when performance or simplicity is needed.

For example, int i = 2 will allocate 32 bits to store the number 2, whilst Integer i = Integer.valueOf(2) will allocate a full object.

If you do i = i + 2, with the int, the value inside those 32 bits gets incremented with a single CPU instruction. Fast.

If you do i = i + 2 with the Integer, in reality you're doing i = Integer.valueOf(i.intValue() + 2), calling a method to retrieve the int value, incrementing by 2, and creating a new object with that value.

Primitive types are special data types built into the language; they are not objects created from a class. That, in turn, means that int doesn't inherit from java.lang.Object in any way because only `objects created from a class