- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Reference static field after declaration in Java
The static field variable is a class level variable and it belongs to the class not the class object.
So the static field variable is common to all the class objects i.e. a single copy of the static field variable is shared among all the class objects.
A program that demonstrates referencing the static field variable after declaration is given as follows:
Example
public class Demo { static int a = 7; static int b = a + 5; public static void main(String[] args) { System.out.println("a = " + a); System.out.println("b = " + b); } }
Output
a = 7 b = 12
Now let us understand the above program.
The class Demo contains static variables a and b. The method main() prints the values of a and b. A code snippet which demonstrates this is as follows:
static int a = 7; static int b = a + 5; public static void main(String[] args) { System.out.println("a = " + a); System.out.println("b = " + b); }
- Related Articles
- Can we make static reference to non-static fields in java?
- Initializer for final static field in Java
- How static class Object is created without reference of outer class in java?
- java variable declaration
- Reference to a static method using method references in Java8
- Java variable declaration best practices
- Difference between Definition and Declaration in Java.
- Initialization, declaration and assignment terms in Java
- Class declaration with one method in Java
- Differences between Method Reference and Constructor Reference in Java?
- Static class in Java
- Static variables in Java
- static Keyword in Java
- Static and non static blank final variables in Java
- Can abstract method declaration include throws clause in java?

Advertisements