Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

compiler error

 
Ransika deSilva
Ranch Hand
Posts: 524
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I just have this question.
say if i have a java program like this.
class a{
int x;
public static void main(String ar[]) {
int y;
System.out.print(x);
System.out.print(y);
}
}
This gives only one error saying it is not possible to refer non-static variable inside a static method.
But if you change the print lines like this.
class a{
int x;
public static void main(String ar[]) {
int y;
System.out.print(y);
System.out.print(x);
}
}
It gives 2 errors saying non static refernece and variable y not initialized.
Could you please explain the difference.
Thank you
 
Andres Gonzalez
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's start by understanding the difference between variable x and variable y:
Variable x:
As you can see, x belongs to a class. They are called instance variables. Instance variables are initialized with their default value. So, in this case, the value of x will be 0 (because it's an int). If you'd declared a boolean instance variable, then their default value would be false.
So, instance variables are always initialized with their default value, if you do not specify any.
Variable y:
y belongs to the main() method. variables that belong to method are called local variables (they have many names, but IMO local variables is the most commonly used one). Local variables are not initialized by default. So, if you try to use them without being initialized by you, the compiler will complain.
Notice that the compiler will not raise any error if you don't use a local variable that has not been initialized by you. It'll only complain if it is reachable in you code. In the second example, you're using variable y to print its value, but if you ommit that line, you won't get that error (because you're declaring variable y but you're not using it in your method)
hope this helps
[ October 05, 2003: Message edited by: Andres Gonzalez ]
 
Ransika deSilva
Ranch Hand
Posts: 524
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am cleared with the concept. What I want to know is why do they give only on error when you compile the first program and two errors when you compile the second code.
why the difference?.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34973
379
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sometimes comile errors higher up in the code can mask other compile errors. The static variable is probably considered more severe and javac would stop.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd think it's just a quirk of the compiler.
 
Andres Gonzalez
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry for not understanding your question
 
Saraswathi
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
The compiler gives the error saying
non-static variable x cannot be referenced from a static context System.out.print(x);
becoz x is a class variable and without creating an instance of it you cannot access it .
class xxx{
int x;
public static void main(String ar[]) {
xxx alpha=new xxx();

int y=0;
System.out.print(y);
System.out.print(alpha.x);
}
}
This works fine . and you need to initialize y to a default value.

OR
class xxx{
static int x;
public static void main(String ar[]) {
//xxx alpha=new xxx();

int y=0;
System.out.print(y);
System.out.print(x);
}
}

If the variable x is declared as static then also the code works fine.

They are the rules you see.
hope it helps.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic