Author | Topic: Assignment Problem |
Wai Iu greenhorn |
posted April 17, 2000 05:38 AM
Question:
When I complie the above code, the complier gives a wrong message
|
Suma Narayan ranch hand |
posted April 17, 2000 06:52 AM
Hi Wai, The error goes if you combine the declaration and initialization. i.e static final int SIZE = 10; This is my understanding for the error which you were getting. 1. Both SIZE and MIN_VALUE are class variables. This means 2. Static variables are allocated memory and initialized at 3. So when you initialize with a value in separate statements, NOTE: The value of static variable can be changed by any I hope the above explanation will help you.
|
Sanath Kumar greenhorn |
posted April 17, 2000 08:05 AM
Hi Wai Iu, As per JLS, A field can be declared final, in which case its declarator must include a variable In your code Your declaration doesn't include the initialization. Hence it will give compile time error. you intialize at the declaration time then No probs. Hope this will focus some light on you problem. regds,
|
maha anna bartender |
posted April 17, 2000 10:02 AM
Wai Iu, In your code the 2 executable statements are not inside any block. This is the problem. In java the skeleten of a program is like this
So if you take any executable statement (not declaration or dec and init statements) SHOULD NOT HANG ALONE . They have to be inside a block { } . This is the Exact reason for your compile error. Try to put inside a block surrounded by { }. Then it will be ok. But in your case since those 2 vars are static final they have to be initialized during the class is loaded itself. So they have to be inside a static floating block and it has to come after the declaration of the 2 var. otherwise forward reference compiler error will occur.Try this way.
[This message has been edited by maha anna (edited April 17, 2000).]
|
Manju Swamy greenhorn |
posted April 18, 2000 07:15 PM
Here are some corrections/comments to the Maha reply. You can create an empty file (example EmptyClass.java) with no code and compile, it works!!!. It will not create any class files, since no class definition. Hey What is the use??? Noting. But adds the completeness to definition of Java program skeleton. You can have only one package statement per Java source file. code: You will get the following error message:
If you comment out any one of the package statement, it complies fine. You does not need to have import statement always. You does not required to have class in a Java source file, but does not make any sense when you have no class definition. I will complete agree with you on 'maximum one public class per Java source'. You can not declare the class to both abstract and final. You might already know it, but overlooked at it while posting. It should abstract or final. Here is an example. extends is optional in the class declaration. If noting is specified it extends from Object class. You can have 0 or more instance and/or static methods. Note: My changes are in green color.
Any comments are most welcome. [This message has been edited by Manju Swamy (edited April 18, 2000).]
|
maha anna bartender |
posted April 18, 2000 07:43 PM
Ok. Manju Swamy, Thanks for correcting. Yes. I didn't really give importance to that skeleten part. Your corrections are very welcome. For the previous post what was on my head was , I had to explain to Wai Iu that those 2 statements are left alone. Please put it into a block.. . SO I really didn't concentrate on the skeleton part. [This message has been edited by maha anna (edited April 18, 2000).]
|
| | |