Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use nested class in another class in java?

Tags:

People also ask

How do I call a nested class from another class?

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.

How nested class and inner class are helpful in Java explain with example?

Using the nested class will make your code more readable and provide better encapsulation. Non-static nested classes (inner classes) have access to other members of the outer/enclosing class, even if they are declared private.

What is the scope of a class nested inside another class?

What is the scope of a class nested inside another class? Explanation: It depends on the access specifier and the type of inheritance used with the class, because if the class is inherited then the nested class can be used by subclass too, provided it's not of private type.

Can outer Java classes access inner class private members?

If the inner class defined as private and protected, can outer class access the members of inner class? Yes.


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.