1. Java source file structure:
class A{
}
class B{
}
class C{
}
i. If there is no public class, then we can use any name for the above program.
ii. But if one of the classes are public, then the program name must be same as class name. Otherwise there will be compile time error. “class B is public, should be declared in public class B”
class A{
}
public class B{
}
class C{
}
iii. If there are more than one public classes, then compile time error will appear. If we save the following file as B.java then the error will be “class C is public, should be declared in public class B”
class A{
}
public class B{
}
public class C{
}
iv. We compile a java program but we can run a java class. If we save the following program as sajib.java, there will be no problem in compiling, but it wont run and give the error NoSuchMethodError: main. When we compile a java program, for every class present in that program, a separate .class file will be generated. So, the following program will generate three .class files such as A.class, B.class, C.class and we can individually run each class file.
class A{
public static void main(String[] args){
system.out.println("A");
}
}
class B{
public static void main(String[] args){
system.out.println("B");
}
}
class C{
public static void main(String[] args){
system.out.println("C");
}
}
IMPORT: There are two types of imports
a. Explicit class import (Ex. java.util.ArrayList)
b. Implicit class import (Ex. java.util.*)
import java.util.ArrayList; // valid import java.util.ArrayList.*; // invalid, no meaning of * after class name import java.util.*; // valid, implicit import import java.util; // invalid
problems in importing..
import java.util.*;
import java.sql.*;
class Test{
public static void main(String[] args){
Date d = new Date();
}
}
// This will give compile error. "reference to Date is ambiguous". Because Date is present in
both util and sql package.
The two package java.lang and current working directory is available to every program by default, i.e. we dont need to import these two.
Static Import:
import static java.math.sqrt; // example of static method import
class Test{
public static void main(String[] args){
sysout( sqrt(9) );
}
}
2. Class level modifiers
3. Member level modifiers 4. Interfaces
Leave a Reply