user3211917
user3211917

Reputation:

calling specific variable from one method to the main class

How to call a variable from a method by a class?

Code sample;

public class House{
public static void main(String args[]){

 system.out.println(Name);



}

public void Types() {

 String Name = House
 int number = 1;

 String Name = Flat
 int number = 2;
}

}

I want to call the variable Name from my Types method to the main class. ---- The code above is completely random and does not have any main purpose, I just want to know how to call a single variable from a method by a class. ----

Upvotes: 0

Views: 88

Answers (1)

vasu
vasu

Reputation: 516

Name here is a local type, it cant be referenced in main method.

Try making a class member example

package com.test;

public class Test {
    public static void main(String args[])
    {
        B b = new B();
        System.out.println(b.name);
    }
}

class B
{
    String name="test";
}

Hope this helps.

Upvotes: 1

Related Questions