What are class members in Java

Using "Static" in Java

Typically, a class member needs to be accessed in connection with an object of its class. There will be times when you want to define the class member that can be accessed without instantiating that class. This can be achieved through "static". To create such a member, you must prefix its declaration with the keyword "static".

Advantages of static

When a member is declared static, it can be accessed before objects of its class are created or without referencing an object.

Why do we declare main () static?

main () is the entry point of a class. In Java everything is written in one class. Now when you run the java filename at the command prompt, the loader loads the class and the JVM searches the main method to enter the class. So if you set main () as static, the JVM can access it directly through classname.main ().

Because of this, the program name must match the class name in which we wrote the main function.

Important points to keep in mind

  1. Instance variables declared as statically are essentially global variables. When you create objects of its class, a copy of a static variable is not made. Even all objects have the same static variable.
  2. static variables or member functions are loaded during the lesson. That is, before creating instances (objects), the main function is the first executable function of a program that we run manually.
  3. If you want to run / initialize something before actually loading the class, put all of these in a static block.



System.out.println ("Static block initialized ...");


It is already executed before the main () method when the class is loaded.

Limitations when using Static

  1. The static method can only access static variables.
  2. The static method can only call the static method.
  3. The static method can't relate to it and is in no way super.


class addition {static int num1; static int num2; int a; int b; Addition () {num1 = -1; num2 = -1; a = -1; b = -1; } Addition (int num1, int num2, int a, int b) {this.num1 = num1; this.num2 = num2; this.a = a; this.b = b; } static void sum () {//System.out.println("Sum of 4 numbers is: "+ (num1 + num2 + a + b)); / * static method can't access static vars * / System.out.println ("Sum of 'num1' and 'num2' is:" + (num1 + num2)); System.out.println (); } void add () {System.out.println ("Addition of 4 numbers is:" + (num1 + num2 + a + b)); System.out.println (); }} class AditionDemo1 {public static void main (String args []) {System.out.println (); System.out.println ("Static method sum () called without creating any object"); System.out.println ("------------------------------------------- ----------- "); Addition.sum (); Addition ad1 = new addition (); System.out.println ("Static Method sum () is invoked second time"); System.out.println ("------------------------------------------- ---- "); Addition.sum (); System.out.println ("Non-Static Method add () is invoked using Object ad1"); System.out.println ("------------------------------------------- ---- "); ad1.add (); Addition ad2 = new addition (10,20,30,40); System.out.println ("Static Method sum () is invoked second time"); System.out.println ("------------------------------------------- ---- "); Addition.sum (); System.out.println ("Non-Static Method add () is invoked using Object ad2"); System.out.println ("------------------------------------------- ---- "); ad2.add (); System.out.println ("Static Method sum () is invoked third time"); System.out.println ("------------------------------------------- ---- "); Addition.sum (); System.out.println ("Static Method sum () is invoked using Object"); System.out.println ("------------------------------------------- "); ad1.sum (); ad2.sum (); }}


Explanation of the Java code and the output

If you read the code carefully, you will find that we have taken two static variables and two non-static variables which are initialized by two constructors, viz. One is parameterized and another is standard. We have defined a static and a non-static method.

In main () we first called the static method sum () without creating an object of the addition class.

In the code you can see that the static method cannot access non-static variables. When you try to access it you get the following error like this:

If you see now, static variables are just global variables. Regardless of the objects, it produced the same result when the static method sum () is called by objects.

Non-static methods can access static variables.

Non-static methods can call static methods.

Static methods can also be used with . and with . .

With non-static variables, each object has different copies of non-static variables with different values ​​for different objects.

Next we will learn about it Using the keyword "this" in Java.

More useful tutorials and definitive guidelines on Java programming can be found here.