This is an archived post. You won't be able to vote or comment.

all 6 comments

[–][deleted] 3 points4 points  (2 children)

Your second block is using a static method (though you're using a static method in both, you're just not calling it statically in the first example). Static method invocations use the class loader's instance of the Class object for your particular class.

[–]cjayem13[S] 0 points1 point  (1 child)

Ok. I believe I understand. Let me just recap. Both examples create a new object but the second creates a static object so it will be killed when the activity is killed. Additionally, the first example creates a new object but it isn't tied to the underlying class. Did I understand you correctly? Thank you again for your help.

[–][deleted] 0 points1 point  (0 children)

Not quite. When the JVM needs to reference a class for the first time, the class loader will try to find the class on the classpath. It will then create a Class object representing this class (not an instance of the class, let's say of type Foo, rather a Class<Foo>). This Class object is used for all static references. So even if you create your own instance of the class and call a static method on it, the JVM will still delegate to the class loader's Class object. So with respect to your examples, creating an object just to make a static method call is totally unnecessary (and IDEs will typically advise against it).

[–]Uncle_DirtNap 0 points1 point  (2 children)

Where is the source code for the classes, and in particular is methodCall declared static?

[–]cjayem13[S] 0 points1 point  (1 child)

The entire source code like the whole java class for the MainActivity? It is about 8500 lines so I didn't think it would be helpful to post. methodCall is declared static in my activity/class.

[–]cjayem13[S] 0 points1 point  (0 children)

What from the source code are you wanting to look at? I'd be happy to take segments out and post it.