Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

A private instance field can only be accessed from non-static methods?

 
nemo zou
Ranch Hand
Posts: 35
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got confused about this question in K&B mock exam. In option C and D, whether a method is static or not would affect its accessibility of a private value. Who can show me one example that shows C is the right answer but D is wrong? Thanks!
static private.png
[Thumbnail for static private.png]
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can NEVER access an instance field directly from a static method. NEVER!



Hope it helps!
Kind regards,
Roel
 
Henry Wong
author
Marshal
Pie
Posts: 22119
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:You can NEVER access an instance field directly from a static method. NEVER!


That's true, but the answer(s) never mentioned directly or indirectly. Why can't the static method be provided an instance of the class, that it can use to access the private field?

Henry
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Roel De Nijs wrote:You can NEVER access an instance field directly from a static method. NEVER!


That's true, but the answer(s) never mentioned directly or indirectly. Why can't the static method be provided an instance of the class, that it can use to access the private field?

Yes, there's nothing wrong with this codeBoth statements will compile successfully. But is the static method staticMethod2 really accessing the private instance field now? Or is it just accessing the (public) instance method instanceMethod (which is accessing the private instance field)?
 
Henry Wong
author
Marshal
Pie
Posts: 22119
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:
Yes, there's nothing wrong with this codeBoth statements will compile successfully. But is the static method staticMethod2 really accessing the private instance field now? Or is it just accessing the (public) instance method instanceMethod (which is accessing the private instance field)?

Actually, I was thinking of something closer to this...
Henry
 
Julius Balt
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Roel De Nijs wrote:
Yes, there's nothing wrong with this codeBoth statements will compile successfully. But is the static method staticMethod2 really accessing the private instance field now? Or is it just accessing the (public) instance method instanceMethod (which is accessing the private instance field)?

Actually, I was thinking of something closer to this...
Henry


It is not possible to access any variable of instance from static context. You need to have an object;
If you have an object, it's possible to access private field of same class, while method and private field are in same class, because private fields are visible only within the class.

 
Guillermo Ishi
Ranch Hand
Posts: 789
C++ Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that is a beautiful example of an awful question/answer.

Perfectly happy code:
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guillermo Ishi wrote:I think that is a beautiful example of an awful question/answer.

I don't agree with you. This is not an awful question. It's at most an illustrative example of how difficult it is to create unambiguous questions and answer options.

Currently you have two types of question: questions with a code snippet asking for the output/result and questions consisting of text only. The first group is crystal clear all the time, because when in doubt, you'll compile and run the code snippet, and you have the correct answer. But with the text-only questions it's a lot harder to reach the same level of clarity and unambiguousness. Because you have plenty of words and each of them can be interpreted differently.
Such questions will also appear on the actual exam, so it's a good thing they are present in the mock exams as well. And each of these questions always has some context. And when reading the question and the possible answers, you can determine this context (every time) and then you probably know the topic you are tested on as well.

Let's illustrate with another example about this question. This topic is about answers C and D, but until now nobody wondered why A is considered to be a correct answer. Why? That's really easy: we all know that a public field can be accessed and modified from anywhere! But is this really always true? No, it isn't! It depends on the access modifier of the class and other possible modifiers. Illustrated in the following code snippetsBoth counter variables are public, but Ex1.counter can only be accessed and modified in the cert package (because class Ex1 has default access). And Ex2.counter can only be accessed from anywhere, but not modified because it's a final variable.

So why is A definitely a correct answer? Because of this question's context! None of the choices mention the final keyword, but public, protected and private is mentioned so it's probably about the visibility. In some choices instance and static is used, so probably testing that knowledge as well. If they were testing your knowledge about final, you would probably see soms choices where "a final variable" and "a non-final variable" are used.

And finally, remember that the real exam tells you how many answers are correct. Which means you can figure out about edge case answers that way.

Hope it helps!
Kind regards,
Roel
 
Guillermo Ishi
Ranch Hand
Posts: 789
C++ Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is a terrible question/answer and there's no getting around it. The supposed wrong answer says it can't be accessed from a static method, yet I demonstrated accessing it in a static method. Very simple. QED. The year I graduated from high school my state was last in the nation. My county was last in the state. My gang cut school to watch Jeopardy on TV, to get an education. I am highly attuned to poorly written test questions.

P.S. About a year ago I discovered MOOCS. I started taking a bunch of things offered by MIT, just because of the reputation. I discovered, wow, these guys are smart enough to not write ambiguous questions, and I can actually trust them. It was a revelation.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guillermo Ishi wrote:The supposed wrong answer says it can't be accessed from a static method, yet I demonstrated accessing it in a static method. Very simple. QED.

So there is definitely a difference in both our test taking abilities (probably due to a difference in experience). Like you, I'm totally aware of a private instance member being accessible from a static method through an instance of the class that owns that field. But in this question I would never select D as a correct answer.
And the same applies to answer A as well. Although I could provide 2 examples (I'm sure you knew them as well) disproving the statement, I would always select A as a correct answer in this question.

Sharing some advice:
1/ Do not assume things they don't tell you. (e.g. if a question tests my knowledge about the final keyword, the statement will explicitly mention something like "a public final field...")
2/ Do not read into the question. (e.g. you know you can't access an instance field/method from a static method, very simple and easy, really basic knowledge)

Or what do you think about this next question? A private instance field can only be accessed from within the class that owns that field. True or false? I think it's safe to say, that's a very clear, unambiguous statement. Probably 99.99% of all exam takers will select the true option (and wonder why not every exam question could be as simple as this one ). But not me! For me that statement is wrong, I know I can use reflection to access any private field from another class

(Disclaimer: reflection is definitely not on the OCA exam and I used it only to illustrate you can - almost - always find an example disproving any statement, so you should not read into the question)
 
Guillermo Ishi
Ranch Hand
Posts: 789
C++ Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:you can - almost - always find an example disproving any statement,

That would be very troubling then

Your statement means the correct way to write the private variable question would be "in the code below..." so that there is clearly no reflection involved, and so the answer is no, the private variable can't be accessed outside the class.

If you can almost always disprove any statement, then what you MUST almost always do is have the question relate to a specific snippet of code. I gave an example that makes D. true, when the correct answer is false, which wouldn't be possible if the question was about a specific bit of code.

BTW, C and D are both awkward English, which exacerbates the problem, and evidences some lack of thought about the question in general. "Owns" should be "own", for starters. People don't owns houses.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guillermo Ishi wrote:BTW, C and D are both awkward English, which exacerbates the problem, and evidences some lack of thought about the question in general. "Owns" should be "own", for starters. People don't owns houses.

I think you need to read more carefully if you're going to critique. The phrase in both C and D is "methods of the class that owns that field". The thing doing the owning is the class, not the methods. Class is singular, so "class that owns" is correct.

For the original question, I agree the mock exam question is poorly phrased. It should have said "directly access" rather than simply "access". Oh well.
 
Guillermo Ishi
Ranch Hand
Posts: 789
C++ Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's probably what they mean. Strunk & White would be all over it anyway
 
Henry Wong
author
Marshal
Pie
Posts: 22119
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I'll chime in ... and agree with Roel, even though I was technically on the other side of the debate...

During a test, the answers forms part of the context. If two answers look right, then you have to choose the one that is "more" right, which is based on experience. And if you can't tell which is more correct, then I guess the question did accomplish its task in testing your level of experience.

Henry
 
Guillermo Ishi
Ranch Hand
Posts: 789
C++ Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know about "more right". An answer is either right, or something in it makes it wrong.
By context, do you mean in ocajp context private variables are only accessible to that class, because ocajp doesn't know about reflection yet?
 
Paul Anilprem
Enthuware Software Support
Ranch Hand
Posts: 3819
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
During a test, the answers forms part of the context. If two answers look right, then you have to choose the one that is "more" right, which is based on experience. And if you can't tell which is more correct, then I guess the question did accomplish its task in testing your level of experience.

Henry

It is really based on the frame of mind of the question setter. I can tell from our experience in setting thousands of questions that in many cases users come up with an entirely different interpretation of the question and that makes correct options seem off. Yet, when you read the explanation, the question seems very straight forward. This happens because when you set a question, you are trying to test the user on one concept and your mind blocks off other possible scenarios. The user doesn't know that while reading just the question and is easily able to see it from a different perspective. The "accessing private variable" issue mentioned above is a good example. The question setter probably didn't even think of that but an experienced developer, who might be taking the test just to get the certification can easily be deceived into selecting the wrong option.

Add to this the fact that people with different mother tongues all over the world take this test in English. One of our users recently got thrown off by the statement, "Which design pattern is employed by the following code?" He thought the question is about being employed and not about being used and so thought that a Factory pattern is used to employ a DAO pattern. The author certainly didn't think that the word employed could be interpreted so differently.

Anyway, the authors of the real exam and authors of popular books don't have to worry much about this problem because their interpretation will always be the right interpretation
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guillermo Ishi wrote:I don't know about "more right". An answer is either right, or something in it makes it wrong.

You must find it difficult to talk to normal humans. ;)

Guillermo Ishi wrote:By context, do you mean in ocajp context private variables are only accessible to that class, because ocajp doesn't know about reflection yet?

I think it's fair to say that the exam authors were well aware of reflection, but when they ask about accessibility, they mean, what is accessible without using reflection. Much like the way the term "accessible" is defined and used in the JLS. Yes, it's possible to use reflection to circumvent this - but that doesn't change what we mean when we say a field is accessible.
 
Liutauras Vilda
Bartender
Pie
Posts: 2802
112
BSD VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Everyone can learn questions and answers like a poems without full understanding. Such a questions are structured like that on very clear purpose. As some guys correctly mentioned - purpose is to test experience. If you're experienced, these "ambiguities" within questions/answers will be eliminated by yourself.
If not - it means need to study on the topic more. And this is a positive side, you'll get more knowledge and full understanding.
 
Guillermo Ishi
Ranch Hand
Posts: 789
C++ Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike Simmons wrote:
You must find it difficult to talk to normal humans. ;)

Normal humans, no. People like you, possibly ;)
 
Guillermo Ishi
Ranch Hand
Posts: 789
C++ Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:Everyone can learn questions and answers like a poems without full understanding. Such a questions are structured like that on very clear purpose. As some guys correctly mentioned - purpose is to test experience. If you're experienced, these "ambiguities" within questions/answers will be eliminated by yourself.
If not - it means need to study on the topic more. And this is a positive side, you'll get more knowledge and full understanding.

I think there is agreement that the question has problems.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guillermo Ishi wrote:By context, do you mean in ocajp context private variables are only accessible to that class, because ocajp doesn't know about reflection yet?

Paul Anilprem's post is absolutely spot-on! Probably because they have already created a fair share of mock questions and have great experience about creating questions and possible different interpretations of the question (and/or the answer options). So yes, when I would create a question about private variables for OCA or OCP exam, I would not consider reflection because it's not an exam topic on these exams. So that's really a no-brainer.

It could get much more complicated when you have to create a question about an exam topic. Assume I have to create a mock question about the omnipresent access modifiers for my imaginary study guide I decide to create a plain text question which requires to select the correct statements. I create a few statements about protected, private and default access, all about instance fields and instance methods. The chapter about access modifiers clearly mentions: public = accessible by the whole Java Universe. So with that statement in mind, I create the following statement as well: A public field can be accessed and modified from anywhere. For me that's a crystal-clear correct statement! Certainly in the context of this question: it's only about a instance fields/methods and the different access levels.
But then you are reading this question (thanks for buying my study guide btw ) and you think: if this field is final, it's not modifiable at all, so the statement is incorrect. Someone else might think something else: if this field is in a class with default access, then the fiield isn't accessible from every class, so this statement is incorrect as well. So my crystal-clear correct statement is for other people an incorrect statement because they have a different interpretation or thought about other scenarios (which I didn't consider when creating the question). And these other interpretations are not wrong in the Java language perspective (if you would make such a remark during a job interview, I would give you ++1 ), but in the context of this question it's still a correct statement.

That's why this advice is very important: NEVER assume something which is not mentioned in the question. So if the question (or answer option) mentions "public field", don't consider a public field in a class with default access nor about the field being final. The question (or answer option) will provide additional information if needed, so there would be an answer option like "a public field in a package-private class can be accessed from anywhere" or "any final instance field must always be initialized using a constructor".
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:Such a questions are structured like that on very clear purpose. As some guys correctly mentioned - purpose is to test experience. If you're experienced, these "ambiguities" within questions/answers will be eliminated by yourself.
If not - it means need to study on the topic more.

I don't fully agree with you. I have done several certification exams, each one with a different level of experience. When I took my first certification exam (SCJP 1.4) I had approximately 1.5 years as a Java programmer. With my most recent exam (OCAJP7) I have been a Java developer for 10 years.

And it's of course true: the more experienced you are (both in Java and in test taking), the easier it will be to answer some questions. But the opposite is true as well: because you are more experienced, some questions are harder to answer than when you would have been a Java programmer with just 1 year of experience. And that's exactly what happened when I took the OCAJP7 exam: some questions were a little harder due to my experience, sometimes I was reading into the question (because you know several scenarios where a correct answer would be incorrect because you are not adhering to the question's context).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic