• 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:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

static reference question  RSS feed

 
author and deputy
Bartender
Posts: 3150
  • Mark post as helpful
  • send pies
  • 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
  • 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: 2373
Java MySQL Database Spring
  • Mark post as helpful
  • send pies
  • 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).]
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!