Você está na página 1de 2

Access Modifiers & declaration

> A java source file can contain any number of class & java source file
can be any name.
> A java Source file can contain Atmost one public class. if
the source file have any public class then name should be same
as public class otherwise we will get compile time error saying
case 1: if there is no public class we can give any name to java
source file .
case 2: if there is one class(ex-class B) declared as public
then the name of the public class & name of the java
program (java source file)should be matched otherwise we will get
compile time error saying "class B is public, should be declared
in file named B.java"
case 3:if class B & C are public and the program name is B.java
then also we will get a compile type error saying
"class C is public, should be declared in file named C.java"
> if the class does not contain main method then we will get
runtime excepton saying "NoSuchMethodError:main"
> if we are trying to execute a java class and corresponding
.class file is not available we will get a runtime exception
saying "NoClassDefFoundError:main"

we can resolve the Problem (ArrayList l=new ArrayList();)


by using fully Qualified Name(java.util.ArrayList al=new java.util.ArrayList()
)
for problem is that using fully qualified name everytime is it
increases the length of the code so it decreases the
readability of code. we can resolve nthis problem by using
import statement.
> two types of import statement
1.explicit class import
2.implicit class import
Explicit class import:
ex- java.util.ArrayList. it increases the readability of code.
Implicit class import:
ex-java.util.*; it decreases the readabilty of code.

In java there are two case where we get Ambiguity problem


in Case of Date & List.
Date are in present in java.util package and in java.sql
List are in present in java.util package and in java.awt package.

while resolving class names, compiler always give the


presedence in following order.
1.Explicit class import
2.Class present in current working directory (default package)
3.Implicit class import

fully qualified statements take less time to compile but short names with
import statement take more time both case have same execution time.

diff b/w #include in c & import statement in java


in C language #inclue all the header files loaded at the time of include
statement.---- static loading
in java there is no .class will loaded at the time of import statement
.class fill will loaded whenever we are using particular class then
corresponding class will be loaded-- dynamic loading or load on demand

12441559611719

Você também pode gostar