Search code examples
javaperformanceencapsulation

Java: Differences in Encapsulation layout?


I'm still a bit knew to Java. Only done PHP and a bit of javascript most my life. I had a question concerning the order you have your classes/methods encapsulated inside each other. Does it matter? Is there a better professional way with doing this? Sorry for the newbie question.

Method 1 (class after main())

public class myClass {
        public void main(String[] args) { 
            //Body Code Here
        }
    }
class myClass {
     //Body Code Here
}

Method 2 (class inside main())

class mainClass {
    public void main(String[] args) { 
        //Body Code Here
        class myClass {
            //Body Code Here
        }
    }
}

Method 3 ( class before main())

class myClass {
     //Body Code Here
}

class mainClass {
    public static main(String[] args) { 
     //Body Code Here
    }
}

Solution

  • You need to have all your methods inside the body of your class. Outside it, you can only import some libraries for example java.util.Scanner(for reading from the console). And you can insert methods before or after the Main method, both ways when you run the program they will be read.