Core Java

Calling Getclass From a Static Context Example

1. Introduction

In Java, the static keyword may be applied to an inner class (a class defined within another class), method, or field (a member variable of a class). A static context and non-static context refer to the scope in which variables, methods, and blocks of code exist and operate. A static context is associated with the class itself and shared across all instances of that class. A non-static context, also known as an instance context, is associated with a specific instance of a class. Instance members require an instance of the class to be accessed and can hold unique values per instance. The java.lang.Object has the getClass method that returns the runtime class of this object, therefore, the getClass() method must be called from an object instance. In this example, I will demonstrate getclass static context with a simple Java project.

2. Setup

In this step, I will create a Java project in Eclipse IDE with two simple Java classes: OuterClass and CallingGetClass.

getclass static context project
Figure 1. Calling GetClass Project

3. OuterClass

In this step, I will create an OuterClass.java and it has a static inner class – MyStaticClass.

OuterClass.java

package org.zheng.demo;

public class OuterClass {

	private static int staticIntValue = 100;

	static class MyStaticClass {
	}

	static int getStaticIntValue() {
		return staticIntValue;
	}

}

4. Getclass Static Context

In this step, I will create a CallingGetClass.java with three methods: staticMethod, fixedStaticMethod, and nonStaticMethod.

CallingGetClass.java

package org.zheng.demo;

public class CallingGetClass {

	private String instanceVar = " default";
	private static String staticVar;

	static {
		staticVar = " Class level data";
	}

	public static void main(String[] args) {
		CallingGetClass demoObj = new CallingGetClass();
		System.out.println("demoObj.getClass()=" + demoObj.getClass() + demoObj.instanceVar);
		System.out.println("CallingGetClass.class=" + CallingGetClass.class);
		System.out.println("CallingGetClass.staticVar=" + CallingGetClass.staticVar);

		CallingGetClass demoObj2 = new CallingGetClass();
		demoObj2.instanceVar = " changed";
		System.out.println("demoObj2.getClass()=" + demoObj2.getClass() + demoObj2.instanceVar);

		System.out.println("OuterClass.MyStaticClass.class=" + OuterClass.MyStaticClass.class);
		System.out.println("OuterClass.getStaticIntValue()=" + OuterClass.getStaticIntValue());

		demoObj.nonStaticMethod();
		System.out.println("demoObj.staticVar()=" + CallingGetClass.staticVar);
		System.out.println("denoObj.instanceVar()=" + demoObj.instanceVar);

		fixedStaticMethod(demoObj2);
		System.out.println("demoObj2.staticVar()=" + CallingGetClass.staticVar);
		System.out.println("demoObj2.instanceVar()=" + demoObj2.instanceVar);

	}

	public static void staticMethod() {
		System.out.println(this.getClass());
		instanceVar = "Changed inside staticMethod";
		staticVar = "Changed inside staticMethod";
	}

	public static void fixedStaticMethod(CallingGetClass obj) {
		System.out.println(obj.getClass());
		obj.instanceVar = "Changed inside fixedStaticMethod";
		staticVar = "Changed inside fixedStaticMethod";
	}

	public void nonStaticMethod() {
		System.out.println(this.getClass());
		instanceVar = "Changed inside nonStaticMethod";
		staticVar = "Changed inside nonStaticMethod";
	}
}
  • Line 6: define a static member staticVar.
  • Line 14: call getClass from demoObj.
  • Line 15: access class via CallingGetClass.class.
  • Line 16: access static member via CallingGetClass.staticVar.
  • Line 25: call demoObj.nonStaticMethod().
  • Line 29: call fixedStaticMethod(demoObj2) by passing the object as an argument so it can call its getClass method.
  • Line 36, 37: see Figure 2 and Figure 3 for errors detected by the IDE.
  • Line 41. 42: fixedStaticMethod handles the static access by passing an object as an argument.
  • Line 47, 48: nonStaticMethod can access getClass at the instance level

There are two errors detected by Eclipse IDE in the staticMethod as a static method can not access "this" keyword and non-static members.

getclass static context static error
Figure 2. StaticMethod Errors

Hover red x mark, then it will display two detailed messages as Figure 3 shows:

  • Cannot use this in a static context.
  • Cannot make a static reference to the non-static field instanceVar.
getclass static context detail
Figure 3. Detail Error Messages

4.1 Getclass Static Context Demo

In this step, I will run the main application and capture the console output.

main Application Output

demoObj.getClass()=class org.zheng.demo.CallingGetClass default
CallingGetClass.class=class org.zheng.demo.CallingGetClass
CallingGetClass.staticVar= Class level data
demoObj2.getClass()=class org.zheng.demo.CallingGetClass changed
OuterClass.MyStaticClass.class=class org.zheng.demo.OuterClass$MyStaticClass
OuterClass.getStaticIntValue()=100
class org.zheng.demo.CallingGetClass
demoObj.staticVar()=Changed inside nonStaticMethod
denoObj.instanceVar()=Changed inside nonStaticMethod
class org.zheng.demo.CallingGetClass
demoObj2.staticVar()=Changed inside fixedStaticMethod
demoObj2.instanceVar()=Changed inside fixedStaticMethod

5. Conclusion

In this example, I demonstrated the getClass static context from two simple classes. We cannot call getClass from a static context because it is an instance method, but we can call it from a static context by passing it as an argument. Here are the key differences between static and non-static contexts.

Compare ItemsStatic ContextNon-Static Context
Associated withClassInstance of the class
Keyword usedstaticNA
Memory allocationOnce, when the class is loadedEach time an instance is created with the new keyword
Access to static membersYesYes
Access to non-static membersNoYes
Access to the this keywordNoYes

6. Download

This was an example of a Java project which demonstrated the getClass static context.

Download
You can download the full source code of this example here: Calling Getclass From a Static Context Example

Mary Zheng

Mary graduated from the Mechanical Engineering department at ShangHai JiaoTong University. She also holds a Master degree in Computer Science from Webster University. During her studies she has been involved with a large number of projects ranging from programming and software engineering. She worked as a lead Software Engineer where she led and worked with others to design, implement, and monitor the software solution.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button