Declarations and Access Modifiers
Declarations and Access Modifiers
Java
Java
Java
Java
Java
A.class
B.class
C.class
D.class
A;
O/p :- Main class A
B;
O/p :- Main class B
C;
O/p :- Main class C
D;
Runtime Error: NoSuchmethodError:main
Durga; Runtime Error: NoClassDefFoundError:Durga
import java.util.*;
import java.sql.*;
class Test
{
public static void main(String[] alt)
{
Date d=new Date();
}
}
Status :- Compiler Error:reference to Date is ambiguous
Note :-Even in the case of List also we will get the same ambiguouty problem
because it available in both java.awt and java.util packages.
While resolving classs names compiler will always gives the priority in the
following order.
1. Explicit class implicit
2. Class present in current working directory
3. Implicit class import
Ex:-
import java.util.Date;
import java.sql.*;
class Test
{
public static void main(String[] alt)
{
Date d=new Date();
System.out.println(d.getClass().getName());//java.util.Date
}
}
Case 5:Whenever we are importing a java package all classes and interfaces
present in that packagesare by default available but not sub package classes.
Ex :- java [Packages]
Util
[Packages]
Regex
[Packages]
Pattern
[Class]
To use Pattern class in our program which import statement is required?
a) import java.*;
b) import java.util.*;
c) import java.util.regex.*;
d) No import is required
Ans:- c
Case 6:The following packages are not required to import because all classes
present in these 2 packages are by default to every program.
a) java.lang package
b) Default packages (current working directory)
Case 7:Import statement is compiled time related concept and if more no. of
import statement present then it will take more time to compile but there will
not be effected on runtime.
Case 8:Difference between C language #include and java language import
statement ?
In the case of C language #include, all specified header files will be loaded at
the beginning only. This is something like static include. But in java language
import statement , no .class files are implemented at beginning whenever we
are using a particular class then only it will be loaded. It is something like
dynamic include or load on demand or load on fly.