I am a new learner of Java. I learned some of the Java core concepts. I got the identifier expected error when run my following code:
class Sekar {
public
Hava a look at Java: Identifier expected :
i = 900;
is a statement as any other. You can't write statement anywhere. It must be in methods/constructors body. Initializing variable in declaration is called definition and is exception to this rule.
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/expressions.html
If you want to initialize static variable you can do it 2 (sane) ways: Initialize variable right where you are declaring it:
class Sekar {
public static int i = 900, j,k;
static void max()
{
j = 100;
if(i>j)
{
k=i;
}
else {
k=j;
}
System.out.println("The maxmimum vale between"+i+"and"+j+"is :"+k);
}
public static void main(String[] args) {
max();
}
}
or do it in static constructor:
class Sekar {
public static int i, j,k;
static {
i = 900;
}
static void max()
{
j = 100;
if(i>j)
{
k=i;
}
else {
k=j;
}
System.out.println("The maxmimum vale between"+i+"and"+j+"is :"+k);
}
public static void main(String[] args) {
max();
}
}
Also, if you want to define a constant I recommend using final keyword.
j could be converted to local variable.
class Sekar {
public static final int I = 900;
static void max()
{
int k;
int j = 100;
if(I>j)
{
k=I;
}
else {
k=j;
}
System.out.println("The maxmimum vale between"+I+"and"+j+"is :"+k);
}
public static void main(String[] args) {
max();
}
}