• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

static reference question

 
author and deputy
Posts: 3150
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
<h5><code>
public class test {
static int x;
static int y;
static final int z=0xaa;
int s=0;

public static void main(String args[]){
System.out.println(x);
System.out.println(y);
System.out.println(z);
new test().hello();
//hello();
}
void hello(){
System.out.println(s);
}
}
</code></h5>
When i tried to run this program it is working good,but when i tried to run the program by uncommenting the line //hello() i'm getting the error
test.java:14: non-static method hello() cannot be referenced from a static context
hello();
^
1 error
Though i can roughly understand the error,I' couldn't able to compeltely understand the error context with JLS reference if available..
 
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ,
The method hello() is a instance method. So you can't reference hello() from static method (main).
Refer JLS 8.4.3.2 static methods
Vanitha.
 
Ranch Hand
Posts: 2379
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
Staic variables can be accessed by both static and nonstaic methods but Non-Staic variables can be accessed by only nonstaic methods while static methods can call only other static methods and nonstatic method can call only other nonstatic mathods.
Am i right?

------------------
azaman
[This message has been edited by Ashik uzzaman (edited July 15, 2001).]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic