• Post Reply Bookmark Topic Watch Topic
  • New Topic

Compilation Error  RSS feed

 
Ivaturi Srinivas
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class x
{

x()
{
int i=0;
static int j;
System.out.println("I::" + i);
System.out.println("J::" + j);
}
x(int k)
{
int i=k;

System.out.println("I::" + i);
System.out.println("J::" + j);
}
}

class y extends x
{

y()
{
super(0);
int k=0;
static int l;
System.out.println("K::" + k);
System.out.println("L::" + l);
}
}

public class Test {
public static void main(String ar[])
{
y s = new y();

}
}

Why am I getting Illeagal start of Expression at line number 6???
And what is the order of initialization of variables/Class varibales when the class Y is instaced?

Thanks
Srinivas Ivaturi
 
Vicken Karaoghlanian
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Why am I getting Illeagal start of Expression at line number 6???

static modifier can not be used inside a method or construstor.

And what is the order of initialization of variables/Class varibales when the class Y is instaced?

Sorry... i didn't quite get your question. Do you mean what is the order in which variables are initialized when object 's' is created?
 
Jeroen Stroom
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ivaturi,
The problem is that you define a static variable in a method. Method variables are stored on the stack and therefor they can't a static.
greetings,
Jeroen
 
Elvis Rocha
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Inside a method, variables may be declared only final.
 
Ivaturi Srinivas
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Everybody
My second question is How the Fileds are intitialized when the class Y is Instanced?
Ex:
Static
Instance variables
Constructor Instance Variables
 
Vicken Karaoghlanian
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
first of all class variables are created upon the creation of the object, then followed by the constructor/method variables in the order they are called.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!