Skip to content

binimbabu/Java-static-method

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Static Methods

package com.bini.babu;

public class JavaStaticMethods {

public static void main(String[] args) {
 System.out.println("Inside Main");
     JavaStaticMethods.method1();
}

static void method1() {
	System.out.println("Inside Method 1");
}

}

method1 is the function name of type void hence doesn't return anything and the method is static. Simply writing static method (i.e static void method1() {System.out.println("Inside Method 1");}) will not invoke the start method we need to call the method1 in main function. Invoking static method is done by this command 'JavaStaticMethods.method1();'. The output will be as follows

Inside Main Inside Method 1

static methods can also be called in static blocks.(like as below)

public class JavaStaticMethods {

public static void main(String[] args) {
	System.out.println("Inside Main");
	JavaStaticMethods.method1();
}

static void method1() {
	System.out.println("Inside Method 1");
}

static {
	System.out.println("Inside the static block");
	JavaStaticMethods.method1();
}

}

Output

Inside the static block Inside Method 1 Inside Main Inside Method 1

About

Java-static-method

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages