This week's giveaway is in the JDBC forum.
We're giving away four copies of Java Database Connections & Transactions (e-book only) and have Marco Behler on-line!
See this thread for details.
Win a copy of Java Database Connections & Transactions (e-book only) this week in the JDBC forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Markus, exam 1 Q51  RSS feed

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What will happen when you compile and run the following code?
public class Scope{ private int i; public static void main(String argv[]){Scope s = new Scope();s.amethod(); }//End of main public static void amethod(){System.out.println(i); }//end of amethod}//End of class
1) A value of 0 will be printed out
2) Nothing will be printed out
3) A compile time error
4) A compile time error complaining of the scope of the variable i
-------------------->
In the official answer, 3 is preferred over 4 because:
"...Thus you will get an error saying something like
Can't make a static reference to a non static variable"
I thought that scope was defined as the set of code locations where a particular variable is usable, and static vs. non-static is one of the determiners of scope, so I would consider the official answer to support option 4 as being superior to option 3.
Is this not what scope means?
 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But you can only refer to a non-static member through an instance of the class.
Static is not a scope, it says that the method/variable is related to the class and not the instance, so how can you refer to an instance variable if you don't have an instance related to it?
That's the point and not scope.
It would work if you changed like this:
public class Scope{
private int i;
public static void main(String argv[]){
Scope s = new Scope();
amethod(s);
}//End of main
public static void amethod(Scope s){
System.out.println(s.i);
}//end of amethod
}//End of class
Ana
 
Ana Abrantes
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's another possible solution:
public class Scope{
private int i;
public static void main(String argv[]){
Scope s = new Scope();
System.out.println(s.i);
}//End of class
Ana
 
Pay attention! Tiny ad!
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!