Author Topic:   Assignment Problem
Wai Iu
greenhorn
posted April 17, 2000 05:38 AM             
Question:

public class aclass {
static final int SIZE;
static float MIN_VALUE;
SIZE = 10;
MIN_VALUE = 10.3f;

void test() {
System.out.println("SIZE="+SIZE);
System.out.println("MIN_VALUE="+MIN_VALUE);}

public static void main(String augs[]) {
new aclass().test();
}
}

When I complie the above code, the complier gives a wrong message
"Identifier expected" at line:SIZE=10. What's wrong with this
code? Does static modifier of SIZE cause this problem or not?
Thanks!



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;
static float MIN_VALUE = 10.3f;

This is my understanding for the error which you were getting.

1. Both SIZE and MIN_VALUE are class variables. This means
there is only one copy of this variable for all instances
of the class.

2. Static variables are allocated memory and initialized at
class load time. As per the JLS, the variable initializer
is evaluated and the assignment performed exactly once,
when the class is initialized. This means, if you have
initialized the variable at the time of declaration, the
variable is initilized with that value at class load
time. But, if you have not initialized, the class
variables ar initialized with default values. So if you
remove the initilization part, the variable MIN_VALUE
will have the default value of 0.0. But since SIZE is
also declared as final, it has to be initialized (final
variables can be initialized at the time of declaration
or in a constructor, not later).

3. So when you initialize with a value in separate statements,
the compiler looks for the identifier and hence you get the
error message.

NOTE: The value of static variable can be changed by any
instance of the class. This change will be available in
all instances of the class.

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
initializer or a compile-time error occurs.

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,
-Sanath

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


//comments can come anywhere
package ... (1 .. n statements)
import ... (1... n statements)
class ....( 1... n classes but maximum 1 public class )

Inside pacakge-level class
--------------------------
(public/default) abstract final {
class vars declaration and initialization

instance vars declaration and initialization

inner class decn (both static and non-static)

inner interface (both static and non static)

//static floating block
static {
}

//instance floating block
{

}

//constructors (1 ....n ctors)


class level (static) methods ( 1.....n methods)

instance level (without static keyword) (1...n )methods

}



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.
 
class aclass {
static final int SIZE;
static float MIN_VALUE;


static {
SIZE = 10;
MIN_VALUE = 10.3f;
}

}


regds
maha anna

[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:

package abc;
package xyz;

class MyClass {}


You will get the following error message:


F:\Java\EmptyClass.java:2: Class or interface declaration expected.
package xyz;
^
1 error

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.
implements is optional in the class declaration.
When no constructors are specified for a class, at the compile time Java compiler inserts a default constructor for the class.

You can have 0 or more instance and/or static methods.

Note: My changes are in green color.



//comments can come anywhere
package ... ( 0 to 1 statement )
import ... (0 ... n statements)
class ....( 0 ... n classes but maximum 1 public class )


Inside pacakge-level class
--------------------------
(public/default) abstract/final extends implements , , ... {
class variables declaration and initialization


instance variables declaration and initialization
inner class declaration (both static and non-static)
inner interface (both static and non static)


//static floating block
static {
}


//instance floating block
{


}

//constructors (0 ... n constructors)

class level (static) methods (0 ... n methods)
instance level (without static keyword) (0 ... n methods)


}


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.
Once again thanks for the follow up.
regds
maha anna

[This message has been edited by maha anna (edited April 18, 2000).]

|