📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
✅ Some premium posts are free to read — no account needed. Follow me on Medium to stay updated and support my writing.
🎓 Top 10 Udemy Courses (Huge Discount): Explore My Udemy Courses — Learn through real-time, project-based development.
▶️ Subscribe to My YouTube Channel (172K+ subscribers): Java Guides on YouTube
1. Java static Variable
static variable example
package net.javaguides.corejava.variables;
public class StaticVariableExample {
public static void main(String[] args) {
Student student = new Student(100, "Student 1");
Student student2 = new Student(101, "Student 2");
Student student3 = new Student(102, "Student 3");
Student student4 = new Student(103, "Student 4");
System.out.println(" ------------ Student 1 -------------");
System.out.println(student.toString());
System.out.println(student2.toString());
System.out.println(student3.toString());
System.out.println(student4.toString());
}
}
class Student {
private int rollNo;
private String name;
private static String college = "ABC"; // static variable
public Student(int rollNo, String name) {
super();
this.rollNo = rollNo;
this.name = name;
}
@Override
public String toString() {
return "Student [rollNo=" + rollNo + ", name=" + name + ", college=" + college + "]";
}
}
Student [rollNo=100, name=Student 1, college=ABC]
Student [rollNo=101, name=Student 2, college=ABC]
Student [rollNo=102, name=Student 3, college=ABC]
Student [rollNo=103, name=Student 4, college=ABC]
2. Java static methods
Key points
- A static method belongs to the class rather than the object of a class.
- A static method can be invoked without the need for creating an instance of a class.
- A static method can access static data member and can change the value of it.
- A static method can be accessed directly in static and non-static methods.
- For example, the main() method that is the entry point of a java program itself is a static method.
- They cannot refer to this or super in any way.
Java static method Example
package com.javaguides.corejava.keywords.statickeyword;
public class StaticMethodExample {
private static int a = 20;
private static int b = 10;
public static void main(String[] args) {
// static main() method calls static add() method
add();
substract();
}
private static int add() {
return (a + b);
}
private static int substract() {
return (a - b);
}
}
- static swap() method
/**
* Swaps the two specified elements in the specified array.
*/
private static void swap(Object[] arr, int i, int j) {
Object tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
- static fill() method
public static <T> void fill(List<? super T> list, T obj) {
int size = list.size();
if (size < FILL_THRESHOLD || list instanceof RandomAccess) {
for (int i=0; i<size; i++)
list.set(i, obj);
} else {
ListIterator<? super T> itr = list.listIterator();
for (int i=0; i<size; i++) {
itr.next();
itr.set(obj);
}
}
}
- static copy() method
public static <T> void copy(List<? super T> dest, List<? extends T> src) {
int srcSize = src.size();
if (srcSize > dest.size())
throw new IndexOutOfBoundsException("Source does not fit in dest");
if (srcSize < COPY_THRESHOLD ||
(src instanceof RandomAccess && dest instanceof RandomAccess)) {
for (int i=0; i<srcSize; i++)
dest.set(i, src.get(i));
} else {
ListIterator<? super T> di=dest.listIterator();
ListIterator<? extends T> si=src.listIterator();
for (int i=0; i<srcSize; i++) {
di.next();
di.set(si.next());
}
}
}
3. Java static block
package com.javaguides.corejava.keywords.statickeyword;
public class StaticBlockExample {
private static String CONSTANT;
private static int CONSTANT_INT;
static {
CONSTANT = "some constant value";
CONSTANT_INT = 10;
System.out.println("Inside static block ");
System.out.println("static block initialized values :: " + CONSTANT);
}
public static void main(String[] args) {
System.out.println("Inside main() method");
System.out.println("Access constant inside main() method :: " + CONSTANT);
System.out.println("Access constant inside main() method :: " + CONSTANT_INT);
}
}
Inside static block
static block initialized values :: some constant value
Inside main() method
Access constant inside main() method :: some constant value
Access constant inside main() method :: 10
Key points about a static block
- We can’t access non-static variables in the static block.
- Static block code is executed only once when the class is loaded into memory.
- The static block executes before the main method because a static block is executed during class loading.
4. Java static nested class
OuterClass.StaticNestedClass
OuterClass.StaticNestedClass nestedObject =new OuterClass.StaticNestedClass();
public class StaticNestedClasses {
public static void main(String[] args) {
final OuterClass.StaticNestedClass nestedObject = new OuterClass.StaticNestedClass();
nestedObject.innerClassMethod();
}
}
class OuterClass {
public void outerClassMethod() {
System.out.println(" outerClassMethod ");
}
static class StaticNestedClass {
public void innerClassMethod() {
System.out.println("innerClassMethod ");
}
}
}
5. Java static methods in Interface (Java 8 onwards)
public interface Vehicle {
String getBrand();
String speedUp();
String slowDown();
default String turnAlarmOn() {
return "Turning the vehice alarm on.";
}
default String turnAlarmOff() {
return "Turning the vehicle alarm off.";
}
static int getHorsePower(int rpm, int torque) {
return (rpm * torque) / 5252;
}
}
public class TestJava8Interface {
public static void main(String[] args) {
Vehicle car = new Car("BMW");
System.out.println(car.getBrand());
System.out.println(car.speedUp());
System.out.println(car.slowDown());
System.out.println(car.turnAlarmOn());
System.out.println(car.turnAlarmOff());
System.out.println(Vehicle.getHorsePower(2500, 480));
}
}
6. Java static import
import static java.lang.Math.PI;
// class declaration and other members
public double area() {
return PI * radius * radius;
}
All Java Keywords
- abstract Java Keyword
- assert Java Keyword
- boolean Java Keyword
- break Java Keyword
- byte Java Keyword
- case Java Keyword
- catch Java Keyword
- char Java Keyword
- class Java Keyword
- continue Java Keyword
- default Java Keyword
- do Java Keyword
- double Java Keyword
- else Java Keyword
- enum Java Keyword
- extends Java Keyword
- final Java Keyword
- finally Java Keyword
- float Java Keyword
- for Java Keyword
- if Java Keyword
- implements Java Keyword
- import Java Keyword
- instanceof Java Keyword
- int Java Keyword
- interface Java Keyword
- long Java Keyword
- native Java Keyword
- new Java Keyword
- package Java Keyword
- private Java Keyword
- protected Java Keyword
- public Java Keyword
- return Java Keyword
- short Java Keyword
- static Java Keyword
- strictfp Java Keyword
- super Java Keyword
- switch Java Keyword
- synchronized Java Keyword
- this Java Keyword
- throw Java Keyword
- throws Java Keyword
- transient Java Keyword
- try Java Keyword
- void Java Keyword
- volatile Java Keyword
- while Java Keyword
Comments
Post a Comment
Leave Comment