Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Static methods

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is a question from Marcus : ---
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
Ans: is 3 Why?
Can any one explain
 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Static methods can only access static members.
 
Ranch Hand
Posts: 625
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is a compiler error because you have a static method accessing a non static variable:
public static void amethod(){
System.out.println(i);}
variable i is not static.
 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It won't compile here

--public static void amethod(){
System.out.println(i);--
as i is not static & a static method amethod() is trying to access it directly.
Thanks
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Sean Casey:
There is a compiler error because you have a static method accessing a non static variable:
public static void amethod(){
System.out.println(i);}
variable i is not static.


 
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I take it the main method is an exception, since it is in fact able to access non-static vars?
 
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ryan, could you provide an example of what you mean please?
 
ryan burgdorfer
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is an example of what I mean by my previous post:

Notice that the main method is of course static, yet it accesses the non-static variable i AND the non-static method aMethod() of class A...
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ryan
In your example you are creating an instance of class A and then accessing the non static method and variable of that instance which is legal. [You will not be able to do a.i if i was declared private]. It is different from accessing the variable directly. If you refer the variable i directly it is assumed that you mean 'this.i'; 'this' is not present when you are in a static method.
Susmita
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Susmita Ganguly:
Ryan
In your example you are creating an instance of class A and then accessing the non static method and variable of that instance which is legal. [You will not be able to do a.i if i was declared private]. It is different from accessing the variable directly. If you refer the variable i directly it is assumed that you mean 'this.i'; 'this' is not present when you are in a static method.
Susmita



-----------------------------------------------------------------
Even if the variable i is declared private then also we can access it by a.i in our main method.It's fully valid and in the scope of the class even if it is declared private because we are accessing it from within the class itself.
Sharad
 
Susmita Ganguly
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sharad
With the example we were discussing on, are you sure we can access variable i even if it were private ? I am just trying to clear my understanding.
 
ryan burgdorfer
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried the same code with i declared as private, and indeed it still worked as before...
BUT, I tried printing i WITHOUT first creating an instance of class A, and the compiler complained that I was trying to make a static reference to a non-static variable. Interesting...it appears that you can only access non-static variables via objects...I guess this makes sense, since without a reference to an object of class A, the variable i is implicitly static.
 
Don't listen to Steve. Just read this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic