PDA

Click to See Complete Forum and Search --> : Need help with test quesiton......


Dillinger4
Jun 3rd, 2001, 11:16 AM
Hi everyone! I hope everyone had a fun memorial day weekend.

Ive been studying for this Java Cert test and the testing software
that came with the book as a question that i really dont understand. Any help on clarifying this question would be great.

Which of the following are true about this variable declaration?

private static int i = 3;

a.) The value of i may not be changed after being assigned a value.
b.) i may only be update by a static method
c.) the value of i is shared among all instances of the class in which it was declared.
d.)i may only be accessed in within the class in which it was declared.


they say that the answer is c & d. But i dont understand how this is possible. d makes sense but c does not. I could see if one is thinking in terms of encapsulation than you would have a public get method that would be invoked to retrieve the value of a private field variable after an instance of the class is created. But to directly access a private field variable wouldnt that result in a compilation error?

Dillinger4
Jun 3rd, 2001, 11:33 AM
I tried to access the private field variable and an error occured.
field1 has private access in testfriend
i= tf.field1;

So would the question be wrong?


class testfriend{
private static int field1 = 3;
}

public class Test{
public static void main(String[] args){
int i = 0;

testfriend tf = new testfriend();
i= tf.field1;
System.out.println(i);
}
}

VirtuallyVB
Jun 3rd, 2001, 11:58 AM
I'm glad that your book says the answer is c & d because that is what I chose as I was reading the question.

Let me also say that I've come across many questions which I felt had typographical errors or some other error that would cost you on the exam, and I would trust the Sybex book written by the authors of the exam itself.

The static part
private static int i = 3;
means that c.) the value of i is shared among all instances of the class in which it was declared.
Create two or more instances of the class containing this member (or even before an instance is created since this requires no instance) and do a few i++ and println(i); before and after creating instances and you will see that every instance and even no instance {public static void main(String[] args)} uses this same variable i.

The private part
private static int i = 3;
means that d.)i may only be accessed in typo within the class in which it was declared.
testfriend cannot access this private member directly without a public getter, but Test can access this variable.

Answer a would require the keyword final.
Answer b is just the opposite. A non-static variable can only be accessed via a non-static method (the method within an instance). static variables and methods can be accessed with and without an instance (as you may be familiar with accessing these from within {public static void main(String[] args)}.

Good Luck!

Dillinger4
Jun 3rd, 2001, 02:11 PM
"the value of i is shared among all instances of the class in which it was declared".

but even though a static variable is shared among all instances,
if it is declared private i dont see how it can be accessed without a method.

i think the wording of some of these questions is crap. So i guess what they are getting at is that a pivate static field is shared among all instances but cannot be accessed by an instance of a class.

VirtuallyVB
Jun 5th, 2001, 04:36 PM
private makes other classes have to use getters/setters to access/modify this class's members, but the same class doesn't need get/set.

static shares the same member among all instances of this class (and before an instance is even created--perhaps also called the singleton).

"a pivate static field is shared among all instances of this class but cannot be accessed by an instance of another class without getters/setters."

I heard that there is a place to comment about the wording after the tests, but it's even better. There is a comment for every question if you so desire. I commented on only 1 question.

My comment was about the words exception and error being used interchangeably or whether this is the class Exception or class Error. It wouldn't have affected my certification.

Dillinger4
Jun 5th, 2001, 07:10 PM
Sometimes the way it is explained is a bit confusing. One of my explains a class field as such:

" A class field is associated with the class in which it
is defined, rather than with an instance of the class"

I know that instance fields are initialized by the classes constructor method itself and class fields are initialized my a hidden method i think called <clinit>


quote:
"a pivate static field is shared among all instances of this class but cannot be accessed by an instance of another class without getters/setters."

So are you saying that a class field even though it is not nessary
to create an instance of the class in which it is contained, if an instance is created it is assocate will all instances of that class?

VirtuallyVB
Jun 6th, 2001, 08:50 PM
I don't think I've ever come across the term "class field". The distinction you need is whether the member (variable and/or method) is unique to the class without requiring an instance. I think "class field", "class member variable", "class variable" are all the same. The only difference I am trying to point out here is between a variable and a method.


" A class field is associated with the class in which it
is defined, rather than with an instance of the class"

So are you saying that a class field even though it is not nessary
to create an instance of the class in which it is contained, if an instance is created it is assocate will all instances of that class?


Yes, I am saying that and also that such a field is declared by using the keyword "static".

So the above is why answer "c" is correct.

Regarding I know that instance fields are initialized by the classes constructor method itself and class fields are initialized my a hidden method i think called <clinit>
I think the test is only concerned with knowing that static class member variables (fields) are initialized to a zero or null type value and "method local 'automatic' variables" are not initialized. The term "automatic" has to do with memory allocation and not automatic initialization of values; and non-static (i.e. instance) variables are initialized to zero or null type values when the instance is created. Again, "static" means shared. So "instance fields" and "class fields" are both automatically initialized to a zero type value for primitives or objects to a null value unless your explicit constructor or static block respectively initializes them to a non-zero, non-null value.

The previous paragraph is about fields. I don't want to say that a static method is shared (I don't think that is tested in that manner and I'm not sure what that could mean). The keyword "synchronized" is more related to the concept of sharing methods.

I hope that helps.

Dillinger4
Jun 7th, 2001, 05:55 PM
:) Thank you for giving me a clearer understanding regarding this matter. Sometimes the books tend to complicate things. One more thing. Im taking the Programmer test in two weeks and i just wanted to know if the test stressed awt or swing. Because a lot of the books that i have on the Java test talk about .awt and
nothing about swing.

VirtuallyVB
Jun 8th, 2001, 07:29 PM
I'm pretty sure that there is NO Swing on the Programmer Certification Exam (but don't take my word for it--I'm also sure that Java.Sun.com states the requirements for the exam).

A text I have says, Even Swing components descend from java.awt.Component. All Swing components inherit from javax.swing.JComponent. JComponent is a subclass of java.awt.Component, so it follows that Swing components contain the inherited functionality of their Abstract Window Toolkit (AWT) predecessors.

But this text takes the approach that information overload is better than information underload.

The only other info that I can arm you with is that the components are pretty similar and are usually (if not all) prefixed with a "J". For instance JPanel instead of Panel.

Good Luck on your exam!