How to Access Static Members in Class in Java


In this example, we can see how to access static members. Here we can only access static members of an external class in a static inner class.

Source Code

1 Outer)

package com.beginner.examples;

public class Outer {

	public int x = 10;
	public static int y = 20;

	// This is a static inner class
	public static class Innter {

		public void show() {
			// Only static members of an external class can be
			// accessed in a static inner class
			System.out.println("Only static members can be accessed   y = "+ y);
		}

	}

}

2 Test)

package com.beginner.examples;

public class Test {

	public static void main(String[] args) {
		
		Outer.Innter innter = new Outer.Innter();
		 
		innter.show();

	}

}

Output:

Only static members can be accessed   y = 20
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments