
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
What are the steps to read static members in a Java class?
A static variable gets created at the time of class loading even before the execution of a static block and the purpose of the static block is to assign value to the static variables. A static variable stores a value that is shared between all instances of the class it is defined in and a static block is a section of code that gets executed when class is first loaded. If we want any logic that needs to be executed at the time of class loading that logic needs to place inside the static block so that it will be executed at the time of class loading.
JVM follows below steps to read static members in a class::
- Identification of static members from top to bottom
- Execution of static variables assignment and static blocks from top to bottom.
- Execution of the main method.
Example
public class StaticFlow { static int firstNumber = 10; static { firstMethod(); System.out.println("first static block"); } public static void main(String[] args) { firstMethod(); System.out.println("main method executed"); } public static void firstMethod() { System.out.println(secondNumber); } static { System.out.println("second static block"); } static int secondNumber = 20; }
Output
0 first static block second static block 20 main method executed
- Related Articles
- What are static members of a Java class?
- What are static members of a C# Class?
- What are Class/Static methods in Java?\n
- When are static C++ class members initialized?
- Can a "this" keyword be used to refer to static members in Java?\n
- What are the key steps in read/write from/to a URL connection in java?
- What is a static class in Java?
- How to access the members of a class from another class in Java?
- What are the differences between class methods and class members in C#?
- What are static methods in a Python class?
- What are the differences between a static and a non-static class in C#?
- Static class in Java
- Static Data Members in C++
- Defining static members in C++
- Static Members in Ruby Programming

Advertisements