permaculture playing cards
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
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Method Problem" Watch "Method Problem" New topic

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(){
void g(){
System.out.println(a+" "+ " "+b+" "+ " "+c );


public static void main(String args[]){

palindrome p=new palindrome();


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

Joined: Aug 16, 2005
Posts: 15033

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

Joined: Oct 02, 2003
Posts: 11914

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
I agree. Here's the link:
subject: Method Problem
It's not a secret anymore!