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


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply 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
Bartender

Joined: Aug 16, 2005
Posts: 12921
    
    3

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 7 API documentation
Scala Notes - My blog about Scala
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 9948
    
    6

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



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


Never ascribe to malice that which can be adequately explained by stupidity.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Method Problem
 
Similar Threads
Palindrome no. problem
Palindrome
palindrome program
Palindrome example
finding half palindrome words