#Rule-1
A source file can have only one public class.
It is ambiguous ?
Then what exactly JVM doing with Public class ?
- Actually whenever java program runs JVM look for the public class and inside public class search for main method.
- When we declare multiple classes with public access modifier, JVM can't figure out in what class to search for main method.
How JVM get main method access ?
- Actually, we are giving the access to JVM by writing the access modifier named public.
Public: When we put this access modifier on method then this can be accessed from any class irrespective of the package.
#Rule-2
A java source file can have any number of non-public class.
- I think this statement straightforward.
#Rule-3.
If the source file contains public class then file name must be same as public class.
- I think #Rule-1 has enough explanation for this.
#Rule-4
If th source file doesn't contain public class then there is no naming restriction.
Eg:
Valid
File name: Demo.java
Valid
File name: C.java
Invalid
File name: C.java
Invalid
File name: A.java
Invalid
File name: B.java
A source file can have only one public class.
It is ambiguous ?
Then what exactly JVM doing with Public class ?
- Actually whenever java program runs JVM look for the public class and inside public class search for main method.
- When we declare multiple classes with public access modifier, JVM can't figure out in what class to search for main method.
How JVM get main method access ?
- Actually, we are giving the access to JVM by writing the access modifier named public.
public class MleClass{ public static void main(String args[]){ System.out.println("Hello! There?"); } }
Public: When we put this access modifier on method then this can be accessed from any class irrespective of the package.
#Rule-2
A java source file can have any number of non-public class.
- I think this statement straightforward.
#Rule-3.
If the source file contains public class then file name must be same as public class.
- I think #Rule-1 has enough explanation for this.
#Rule-4
If th source file doesn't contain public class then there is no naming restriction.
Eg:
Valid
File name: Demo.java
class Test{ // Some code }
Valid
File name: C.java
class A{ // Some code } class B{ // Some code }
Invalid
File name: C.java
public class A{ // Some code }
Invalid
File name: A.java
public class A{ // Some code } public class B{ // Some code }
Invalid
File name: B.java
public class A{ // Some code } class B{ public static void main(String args[]){ } }
No comments:
Post a Comment