Menu

use of static keyword in java

Access nonstatic Members From Static Method A static method cannot call a non-static members from it. The only way to call a non-static method from a static method is to have an instance of the class containing the non-static method. The most common example of a static member is main( ). main() is declared as static because … Read more…

Static Methods Like static member variable, we can also have static methods. Static methods do not operate on objects. Static method can access static data member and can change the value of it. A static methods can be accessed directly by the class name and doesn’t need any object. Static methods can only access static variables and static methods. Program … Read more…

Static Block Sometimes, we may wish to assign initialize values to a set of static variables when a class is loaded into memory. In such situations, we shall define and use a block of statements in the form of a static initialization block. Static block is a special type of blocks are blocks defined within … Read more…

Access Static Variables without Object When a member is declared static, it can be accessed before any objects of its class are created, and without reference to any object. Static variables can be accessed directly by the class name and doesn’t need any object. Program Program Source class Data{ static int data1 = 40; static … Read more…

Static Variables Let us assume that we want to define a member that is common to all the objects and accessed without using a particular object. That is, the member belongs to the class as a whole rather than the objects created from the class. Such members can be defined by static keyword. The members that … Read more…