r/javahelp • Nooblet Brewer • Jun 15 '20

I’m having a concept issue with regards to Static / Instance fields

Hey all,

I’m studying for the OCA and came across a concept I’m having a bit of trouble grasping. The textbook is saying that if Class A1 has a public static int myInt = 4, I do not need to create a new instance of Class A1 to use myInt. This I understand. I also get that if it wasn’t a static, I would have to create a new instance of this Class A1 to use this int (if the class I’m in is static).

My issue came when the book said you could create a new object and attach the static method to it.

A1 tester = new A1();
System.out.println(tester.myInt)//should print 4
tester = null;  
System.out.println(tester.myInt)//still prints 4

The last line is confusing me. The book says java doesn’t actually use the object tester so it doesn’t matter whether it has a value or not?? Sorry if this isn’t clear, I just don’t understand how the last line can compile

2 Upvotes

11 comments sorted by

4

u/[deleted] Jun 15 '20 edited Jun 15 '20

The reason why it doesn't throw a nullpointerexception is because static members aren't associated with objects to begin with.

All static members are initialized at the start of code execution. Even if you never created any object of Class A1. You will be able to access myInt.

The book uses an odd method to access static member which is discouraged as it can be confusing. (Probably for demonstration purposes)

The right way is this to use type expression which is

Syetem.out.println(A1.myInt);

As you can see I am not using object at all and whenever java runs your last line. It automatically corrects your code and invoke the line I mentioned above. Since object not being used, you don't get NPE.

Hopefully I was able to explain it. If you find it confusing. Just ask.

1

u/VanVleet-goes-for-22 Nooblet Brewer Jun 15 '20

I think that makes sense. The book does say that to access static methods from other classes you can just do className.fieldName because it doesn’t require an instance. The book is just preparing me for “trick” questions like the block of code I had. I didn’t want to just take the books word for it so I wanted to get a better fundamental understanding.

So static methods / fields AREN’T associated with any objects, only instance fields/Methods are.

I guess my next question would be, why would Java allow me to attach a static member to an object ? Why would tester.myInt even work?

2

u/[deleted] Jun 15 '20

So static methods / fields AREN’T associated with any objects, only instance fields/Methods are.

That is correct.

I guess my next question would be, why would Java allow me to attach a static member to an object ? Why would tester.myInt even work?

It is more of a design choice I guess. It is not like Java doesn't say anything. It gives warnings but it is smart enough to understand what are you trying to do.

1

u/[deleted] Jun 15 '20

Thank you!

The original line System.out.println(tester.myInt) uses an object of the A1 class, which is the cause of my confusion.

2

u/[deleted] Jun 15 '20

Java replaces the line behind the scene

System.out.println(tester.myInt)

With this

System.out.println(A1.myInt)

You are not using object to access myInt.

1

u/[deleted] Jun 15 '20

So instead of accessing the method via an instance reference, it accesses it via the class A1 reference?

2

u/[deleted] Jun 15 '20 edited Jun 15 '20

Yes. All static members and methods and accessed via their class. The static keyword makes them the part of class rather than object.

For example

In System.out.println(); you are accessing out from class System. Directly without an object.

In source code out is

public static final PrintStream out;

Or when you import Math and use Math.PI;

You just call it directly via its class.

1

u/[deleted] Jun 15 '20

Shouldn't the last line, System.out.println(tester.myInt()) throw a null pointer exception?

2

u/VanVleet-goes-for-22 Nooblet Brewer Jun 15 '20

It doesn’t, it still shows 4

I thought it should too, try it out

0

u/[deleted] Jun 15 '20

??? I don't have any class named A1 on my machine...

2

u/garblz Jun 15 '20

See the Java Language Specification, section 15.12.4.1. Compute Target Reference:

When a target reference is computed and then discarded because the invocation mode is static, the reference is not examined to see whether it is null

So, that's discarded on account of being static, and replaced by class reference itself. It's a good idea to use it from the get go like this A1.myInt to avoid confusion. Having said that, a nice catch! It's fun interesting to poke at the internals.