在没有父类帮助的情况下,如何将变量从一个方法传递到同一类中的另一个方法?

Arn*_*Das -1 java variables methods

让我们来看一个像这样的简单程序:

public class Dope
{
public void a()
{
   String t = "my";
  int k = 6;
}
public void b()
{
    System.out.println(t+" "+k);/*here it shows an error of not recognizing any variable*/
}
public static void main(String Ss[])
 {

 }   
}
Run Code Online (Sandbox Code Playgroud)

虽然我可以通过这种方式纠正它:

  public class Dope
{
String t;
  int k ;
public void a()
{
    t = "my";
   k = 6;
}
public void b()
{
    System.out.println(t+" "+k);
}
 public static void main(String Ss[])
 {

 }   
}
Run Code Online (Sandbox Code Playgroud)

但是我想知道以前的程序中是否有任何方法可以在不借助父类帮助的情况下将声明的变量传递method amethod b它?

Car*_*rlo 5

您可以使用两个参数声明b方法,如下例:

public class Dope
{
    public void a()
    {
        String t = "my";
        int k = 6;

        b(t, k);
    }

    public void b(String t, int k)
    {
        System.out.println(t+" "+k);
    }

    public static void main(String Ss[])
    {

    }   
}
Run Code Online (Sandbox Code Playgroud)