Evgeni Shuster

Greenhorn
+ Follow
since Feb 13, 2005
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Evgeni Shuster

I perfectly understand what Alton says.
I just say that it should not work this way.
In first example instance variable initializer executed before super constructor (must be after, according to JLS).
In second and third examples final field accessed before a value is assigned to it. (final field must have PROPER value before first access to it)
It is impossible to do thease things without using super constructor.

Note that Alton says:... it needs a proper initial value before its used ...
He just allows use of i in A, because i is not part of A.
But this allows access to initial (not proper) value of i from B.
But really there is nothing to do here.
Just to learn this problem and take care in our own programs.

to Tommy
All thease is not more evil than your changing of immutable Strings inside JVM. Dangers of such tricks were clear from the begginning.
But thank you for mentioning "Effective Java".
to Alton
According to JLS here
I will copy it for convenience:


Each local variable (�14.4) and every blank final (�4.5.4) field (�8.3.1.2) must have a definitely assigned value when any access of its value occurs. A Java compiler must carry out a specific conservative flow analysis to make sure that, for every access of a local variable or blank final field f, f is definitely assigned before the access; otherwise a compile-time error must occur.



I understand your point.
But I have few more questions.

i is variable of B and is accessed in A, so you allow this
(it's ok, couse JVM allows this too )
then, with some use of polymorphism, we have execution flow that accesses i
A.main() --> B() --> A() --> B.getI()
I think this must be forbidden (as I understand JLS)

lets do one more test
1. add following method to A

2. add following method to B

3. add following line to be first line in constructor B()


Now the program looks like:


It prints:
A.newInstance() : i=0 j=0
un()
i = 0
B.newInstance() : i=1 j=1


here I access i in B before assigning a value to it,
and this looks even more forbidden than accessing it in A

How does this happen?

And one more question: Why in the original example
(posted by Xiaolong KONG) i gets value before call to constructor A() ?
(SomeType)Something means: cast Something to type SomeType,
for example:

here (Object)MyString is of type Object
In your example compiler needs some variable or expression after (Object)
to Mike

i is not class (=static) variable, it is an instance variable.

to Alton

According to the chapter 16 in JLS:
blank final field must have definitly assigned value when any access of it's value occurs
in modified version we access value of i ( in constructor A() ) before we assign value to it ( in constructor B() )

I still think, the reason for wrong behaviour is somewhere in implementation of JVM (some kind of optimization or just bug
[ February 25, 2005: Message edited by: Evgeni Shuster ]
to Animesh
Nobody has problem with STATIC and final variables.
UN is static and final and it is initialized with compile time constant 1.
But i is NOT static, so why i is initialized before constructor A() ?
And how it is possible to use final i in second test even BEFORE the value is assigned to it?
Hello,
I made a mistake, it's so sad
Thank you Xiaolong for noticing it.
is initializer.
And it must be called after A().

May be it is some kind of optimization
UN is compile time constant, and i is final.
May be compiler hints to JVM to initialize i with 1 and not with default 0.
It makes sense in every case except our example.
And test shows that it happens on practice.

Do one more test with the program.
replace line with
then add to be first line of constructor B().
Class B now looks like this:

Compile it and run.
It prints i=0, j=0 for A and i=1, j=1 for B.

Note that i is still final here!
Interesting, we use final variable i even before it is assigned!
(check chapter 16 in JLS)

Where did you get this example? Is there anything else as good as this one?
[ February 24, 2005: Message edited by: Evgeni Shuster ]
When I said good example, I didn't mean coding practice.
It is good example of question to prepair for Programmer Exam.

May be my explanation of why i=1 was too short.
I wrote:


note that UN is known at compile time
and un() is not (it is method !)
so during memory allocation i is initialized with UN=1
and j is initialized with default =0


You should have good understanding of what is "instance variable initializer" and what is not.
In our case:

is initializer, but:

is not initializer, because i is final and UN is compile time constant.
You can find exect definition of instance (and static) variable initializers in JLS (probably somewhere in "Statements" or "Expressions" parts).
Hello.
Very good example.

First, here is your link: http://java.sun.com/docs/books/jls/index.html

Second, about the program.
There is exact explanation for instantiation in Java Language Spec.
(paragraph 12.5 (page 242 in PDF))

1. Memory is allocated.
2a. If explicit constructor of superclass presents (super()) then it is called
2b. If there is no explicit constructor then implicit constructor is called
(default constructor: super())
3. All instance variables initializers are called
(note, that static initializers are called during class loading)
4. Rest of the body of the constructor is executed.

note that UN is known at compile time
and un() is not (it is method !)
so during memory allocation i is initialized with UN=1
and j is initialized with default =0

when you call B() in main()
you execute following
1. execute A()
2. initialize i and j
3. execute B()

and the output confirms that
to Vinicius Boson
You didn't write what exam are you talking about (1.4 or 1.5 beta).

If you want 1.4 then you need voucher and must contact Sun before you will be able to register for exam.

But if you want 1.5 beta then it may be possible to register without voucher.
See
http://www.sun.com/training/certification/java/beta.xml
for details.
In this case you just need to explain to your prometric center that it is not a usual Sun exam, but BETA exam.
You must TAKE the exam before March 6.
It means, that you must REGISTER for the exam few days before you can take it.
For EXAMPLE, I need to register 5 days before I can take the exam.
So, if I take exam on March 3, I must register before Februar 26.
Also test center can have it's own internal problems both with registration and the exam itself.
So, CONTACT YOUR LOCAL TEST CENTER to know exact dates for you.
One more thing, about editor.
I highly recommend JEdit.
It's written on java and for java, so it can run on any platform (including Linux and Windows) and has some useful features for java programming.
You can download it for free at
www.jedit.org
19 years ago
You can check topic:
installation problem in linux
it is posted just 2 lines before yours.
It is information there about setting JAVA_HOME, PATH and CLASSPATH environment variables.
19 years ago
Hello san ch.
First, by posting this you helped me to setup my own JDK, thank you.
Second, you typed that you used exactly:
export JAVA_HOME=/usr/local/sun/jdk1.2.2
If it is so, them you may be need to change it to fit your directory.
For example, I installed JDK 1.5.0 and used:
export JAVA_HOME=/usr/java/jdk1.5.0_01
Please respond, if this was useful.
Good luck.
19 years ago
Hello. I saw some people did beta exams. For example, SCJP 1.5 beta.
Where can I find information about beta exams.
Also, I passed SCJP 1.4 and now I prepair for SCJD.
What version of Java should I use?
Are both versions (1.4.2 and 1.5) allowed?
If yes, which one preffered?