KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I'm studying up for the SCJP exam, upon doing some mock tests I came across this one : It asks what is the output of the following : class TestClass { int i = getInt(); int k = 20; public int getInt() { return k+1; } public static void main(String[] args) { TestClass t = new TestClass(); System.out.println(t.i+" "+t.k); } } I thought it would be 21 20 , since t.i would invoke getInt, which then increments k to make 21. However, the answer is 1 20 . I don't understand why it would be 1, can anyone shed some light on this?
Tags (comma-separated)
Save Edits
Cancel