简体   繁体   中英

How do I declare a variable of certain type in Java?

I have this class called Imp.

public class Imp{

}

so how can I have this variable, let's say its called iimmpp, with the type Imp, and it should be able to access globally.

public static Imp iimmpp;

where should I put this statement? Thanks.

on your main class to create an object of imp,

type:

Imp iimmpp = new Imp();

In any class, since it's public and static you can operate with it from any other class with classname.iimmpp;

For instance, in a class named Foo

public class Foo {
    public static Imp iimmpp;
}

You can access it with Foo.iimmpp from any other class on your application

Use this in your main method to create an variable of type Imp. Imp iimmpp = new Imp();

If you just want it to be accessible i the class then you can just write :

public class Imp{
    public Imp iimmpp = new Imp();
}

when you instantiate this class you will still be able to retrieve the variable iimmpp because it has a public access.

Put it in any other Static context where you will need to use it.

You can use it in any class that can see the Imp class (ie has imported the Imp class).

Simply put it between the brackets of the class declaration. You can also put it inside of a static method, but then it will only be accessible within that method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM