4. Save JAVA File:
---------------------------------------------------------------------------------------------------------------
To
save JAVA File, we have to use following conditions:
A.
If the present java file contains any public element [class, interface,
abstract class, enum] then the present JAVA file must be saved with public
element name only, if we violate this condition then compiler will rise an error.
B.
If no pubic element is existed in present JAVA file then it is possible to save
JAVA file with any name like abc.java or xyz.java, but it is suggestible to
save JAVA file with main class name instead of any other name.
EX:
File Name: abc.java
--------------------------------
class
FirstApp {
public static void main(String[]
args) {
System.out.println(“First
JAVA Application”);
}
}
Status:
No compilation error, but not suggestible to save abc.java
EX:
File Name: FirstApp.java
class
FirstApp {
public static void main(String[]
args){
System.out.println("First
JAVA Application");
}
}
Status:
No compilation Error & Suggestible.
EX:
File Name: FirstApp.java
public
class A {
}
class
FirstApp {
public static void main(String[]
args){
System.out.println("First
JAVA Application");
}
}
Status:
Compilation Error
EX:
File Name: A.java
public
class A {
}
class
FirstApp {
public static void main(String[]
args){
System.out.println("First
JAVA Application");
}
}
Status:
No Compilation Error.
Q.
Is it possible to provide more than one public class with in a single java
file?
ANS.
No, it is not possible to provide more than one public class with in a single
JAVA file, because, if we provide more than
one public class with in a single JAVA file, then we must save that java
file with more than one name, which is not possible in OS.
EX:
File Name: A.java
public
class A {
}
public
class B {
}
class
FirstApp {
public static void main(String[]
args){
System.out.println("First
JAVA Application");
}
}
Status:
compilation Error.
No comments:
Post a Comment