Is there any way to instantiate an entire class that is within the main class?

Asked

Viewed 52 times

1

public class mainPrime{

    public static void main(String[] args) {
        //-----------------------
        new mainPrime().instancePrime2();
        //------------------------  
        
    }
    
    public void instancePrime2() {
        new Prime2().iniciarJogador();
    }
    
    public class Prime2{
        
        public void iniciarJogador() {
            System.out.println("jogador iniciado!");
            
        }
        public void renderizarJogador() {
            System.out.println("jogador renderizado!");
            
        }
        public void playerDeath() {
            System.out.println("jogador morreu!");
            
        }
        
    }
    
}

(here in this case, I am instantiating only one Prime2 method on mainPrime, and I wanted to instantiate the entire Prime2 within mainPrime, to use the methods freely, is there any way to do this or not? I’m new to Java and I’m still learning)

  • 3

    "i am instantiating only one Prime2 method on mainPrime" this concept is wrong are not instantiated, only instantiate classes, that is, when making new mainPrime().instancePrime2(); you are instantiating mainPrime and calling the instancePrime2 method. It would be the same as var instancia = new mainPrime(); instancia.instancePrime2()

1 answer

0


Yes it is possible, just that the declaration of its internal class does not have the level of visibility "public".

What you’re trying to do is known as inner class (inner class).

To instantiate an inner class, you must first instantiate the outer class. Then create the internal object within the external object with this syntax:

public class mainPrime{

    public static void main(String[] args) {
        //-----------------------
        mainPrime mp = new mainPrime();
        mainPrime.Prime2 p = mp.new Prime2();
        p.iniciarJogador();
        p.renderizarJogador();
        p.playerDeath();
        //------------------------  
        
    }
    
    //public void instancePrime2() {
    //    new Prime2().iniciarJogador();
    //}
    
    class Prime2{ //remover a palavra "public" nesta linha
        
        public void iniciarJogador() {
            System.out.println("jogador iniciado!");
            
        }
        public void renderizarJogador() {
            System.out.println("jogador renderizado!");
            
        }
        public void playerDeath() {
            System.out.println("jogador morreu!");
            
        }
        
    }
    
}

There are two special types of internal classes: classes locais and classes anônimas.

Note that doing so will no longer be necessary the method instancePrime2(). For we are creating everything within the method main. Then in my implementation I left commented.

Its mainPrime class does not follow the java class name convention, by convenção should be: MainPrime and have the file name MainPrime.java same as class name.

Reasons to use internal classes:

  • It is a way of logically grouping classes that are used only in a place: if one class is useful to just another class, then it is logical to incorporate it into this class and keep the two together. Nesting these "auxiliary classes" make your package more simplified
  • Improved readability and maintenance of code: nesting small classes in upper-level classes brings code closer to where it is used.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.