milind
milind

Reputation: 970

how to use nested class in another class in java?

I have some situation that I want to use inner class of another class in another class. like...

public class ListData {
    public  static class MyData {

        public String textSongName, textArtistName, textDuration, textDownloadPath,
                textSongSize, textAlbumName, textUrl;
        public boolean enable;

        public MyData(String songName, String artistName, String duration,
                String downloadPath, String songSize, String albumName,
                String url, boolean e) {
            textSongName = songName;
            textArtistName = artistName;
            textDuration = duration;
            textDownloadPath = downloadPath;
            textSongSize = songSize;
            textAlbumName = albumName;
            textUrl = url;
            enable = e;
        }
    }

}

now I want to use Mydata class in another.

how can I do this?

Thanks.

Upvotes: 9

Views: 27285

Answers (3)

Akkii
Akkii

Reputation: 21

If you can create inner class object from other class that is out of scope from your enclosing class than you should use its fully qualified name.... Here , your outer class is ListData and your Inner class is Mydata and we call Mydata from another class eg. YourData.. than we can use below statement..

Syntax : outerclass.innerclass = new outerclass.innerclass();

=> ListData.MyData objectname = new ListData.MyData();

NOTE : this can use only when innner class is static..

Upvotes: 2

user467871
user467871

Reputation:

Static Nested Classes

OuterClass.StaticNestedClass nestedObject = new OuterClass.StaticNestedClass();

Inner Classes

OuterClass.InnerClass innerObject = outerObject.new InnerClass();

More about Nested Classes

Upvotes: 22

Vladimir Ivanov
Vladimir Ivanov

Reputation: 43088

ListData.MyData myData = new ListData.MyData();

Upvotes: 23

Related Questions