java static class code example

Example 1: what is static methods and variables

The methods or variables defined as static are shared among all the objects 
of the class. The static is the part of the class and not of the object. 
The static variables are stored in the class area, and we do not need 
to create the object to access such variables. 
Therefore, static is used in the case, where we need to define 
variables or methods which are common to all the objects of the class.
For example, In the class simulating the collection of the students in 
a college, the nameof the college is the common attribute to all the students.
Therefore, the college name will be defined asstatic

Example 2: static in java

The static keyword in Java is used for memory management mainly. We can apply static keyword with
variables, methods, blocks and nested classes. The static keyword belongs to the class 
  than an instance of the class.

The static can be:

Variable (also known as a class variable)
Method (also known as a class method)
Block
Nested class

Example 3: java what is static

In the Java programming language, the keyword static indicates that the particular member belongs to a type itself,
rather than to an instance of that type.
This means that only one instance of that static member is created which is shared across all instances of the class.

Example 4: how to call a static method in java

class scratch{
	public static hey(){
		System.out.println("Hey");
    }
	public static void main(String[] args){
		hey();
        //print Hey to the console
    }

Example 5: Static method in java

We can declare a method as static by adding keyword “static” before method name.
Let’s see example on static method in java.

public class StaticMethodExample
{
   static void print()
   {
      System.out.println("in static method.");
   }
   public static void main(String[] args)
   {
      StaticMethodExample.print();
   }
}

Tags:

Php Example