aspose file tools*
The moose likes Beginning Java and the fly likes Method Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Method Problem" Watch "Method Problem" New topic
Author

Method Problem

Ganesh Kumar
Ranch Hand

Joined: Jul 02, 2007
Posts: 113
Hi friends ,
Why cant we access a method from another method using the instance as shown below

class palindrome{
static int a,b,c;
void j(){
System.out.println("hi");
p.g();
}
void g(){
a=10;
b=20;
c=40;
System.out.println(a+" "+ " "+b+" "+ " "+c );

}

public static void main(String args[]){

palindrome p=new palindrome();
p.j();

}
}


Please clarify my doubt friends
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14269
    
  21

The variable 'p' is a local variable in the main() method. It isn't visible in method j().

See: The Java Tutorial - Variables.

Note: Please use code tags when you post code, that will make your code easier to read in the forums.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11417
    
  16

What would you expect to happen if your main looked like this:



What, exactly, would the p in the j() method refer to?


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
 
 
subject: Method Problem