Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

get variables from another method..

Status
Not open for further replies.

mackey333

Technical User
May 10, 2001
563
0
0
US
I have two methods...
Code:
public static void debug(String debugMethod)
{

    if (debugMethod.equals("getTerms"))
    {
        System.out.println(getTerms.s1);
        System.out.println(getTerms.num);
    }
}

public static int getTerms()
{

     String s1 = "blah";
     Int num = 10;
     debug("getTerms");
}
it says "cannot resolve symbol" with an arrow at the g in getTerms in 'System.out.println(getTerms.s1);'. how do i accomplish this? also does java contain an eval method? -Greg :-Q

flaga.gif
 
The reason why you get the "cannot resolve symbol" message is because when you say, getTerms.s1, the java compiler will look for an object called getTerms and then try to retrieve the s1 attribute of that object. But you don't have such an object, hence the error message.

I don't really understand what you are trying to achieve so I can't really show you what to do. But it looks like there is something wrong with your design.

Maybe you should instantiate s1 and num as attributes of your class and then you could just refer to them as this.s1 and this.num without having to call a method like getTerms.
 
Its supposed to be so that if "debug" is entered at a prompt, the debug method will be called and print out certain variables depending on where it was called. how can i get the variables from the other method? -Greg :-Q

flaga.gif
 
Hi,
to be abble to access your variables from all the methods of a particular class may be a code like this could be written :
public class A
{
private int num ;
private String s1 ;
public int getTerms()
{

s1 = "blah";
num = 10;
debug("getTerms");
}
public void debug(String debugMethod)
{
if (debugMethod.equals("getTerms"))
{
System.out.println(s1);
System.out.println(num);
}
}
public static void main( String [] args )
{
A a = new A();
a.getTerms();
}
}

As jagel stated, I don't really know what you want to do but I hope this code snippet will help you :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top