0% found this document useful (0 votes)
73 views

Declarations and Access Modifiers

1. Java programs can contain multiple classes but only one class can be public. If a class is public, the file name must match the class name. 2. Access modifiers like public, private control visibility of classes and members. Import statements allow using classes without fully qualified names, improving readability. 3. Issues like ambiguous classes from multiple imports can be avoided by explicitly importing just one class or using fully qualified names. Only classes from imported packages are available, not subpackages.

Uploaded by

Sam Altaf
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
73 views

Declarations and Access Modifiers

1. Java programs can contain multiple classes but only one class can be public. If a class is public, the file name must match the class name. 2. Access modifiers like public, private control visibility of classes and members. Import statements allow using classes without fully qualified names, improving readability. 3. Issues like ambiguous classes from multiple imports can be avoided by explicitly importing just one class or using fully qualified names. Only classes from imported packages are available, not subpackages.

Uploaded by

Sam Altaf
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Declarations and access Modifiers :1.

Java Source file structure


2. Class Modifiers
3. Member Modifier
4. Interfaces
1. Java source file structure :- A java program can contains any number of classes but
atmost one class can be declared as public. If there is a public class then the name of
the program and name of the public class must be matched. If there is no public class
then we can us any name for java program file name and there are no restrictions.
Ex :- class A
{}
Class B
{}
Class C
{}
Case 1:If there is no public class then we can use any name for java program
file.
A.java
B.java
C.java
D.java
Durga.java
Case 2:If we declare class B as public then name of the program must be the
B.java otherwise we will get compile time error like class B is public should be
declared in a file named B.java.
Case 3:If we declare both B and C classes as public and name of the program is
B.java then we will get compile time error like class c is public, should be
declared in a file named C.java.
Ex :- class A
{
Public static void main(String[] alt)
{
System.out.println(Main class A);
}
}
class B
{
Public static void main(String[] alt)
{
System.out.println(Main class B);
}
}
class C
{
Public static void main(String[] alt)
{
System.out.println(Main class C);
}
}
Class D
{}

On Command prompt :- javac Durga.java

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

Conclusion:a) We can compile a .java file it is must to run a .class file .


b) Whenever we compile a java program for every class in the program a separate
.class file will be generated.
c) Whenever we are execute a java .class file the corresponding main class method
will be executed.
d) If the corresponding class does not contain main() method then we will get
runtime exception like NoSuchMethodError:main.
e) Whenever we execute a java .class file if the corresponding .class file is not
available then we will get runtime exception like NoClassDefFoundError:Durga.
f) We can take any no. of classes in a program but it is not a good practice. It is
highly recommended to declare only one class per source file and name of the
program must be similar to the name of main class. In this approach we get more
readability and maintainability and the application is improved.
Import Statement :Class Test
{
public static void main(String alt[])
{
ArrayList al=new ArrayList();
}
}
Status :- Compilation Error:cannot find symbol
symbol:class ArrayList
location:class Test
We can solve this compile time error by using fully qualified name.
Java.util.ArrayList=new java.util.ArrayList();
The problem with fully qualified name is it increases the length of the code and
reduces readability. We can overcome with this problem by using import statement.
import java.util.ArrayList;
Class Test
{
public static void main(String alt[])
{
ArrayList al=new ArrayList();
// short name
}
}
Hence whenever we are writing import statement it is not required to use fully
qualified name and we can use short name directly.Hence import statement acts as
best typing shortcut.

Case 1:Types of import statement


There are 2 types of import statement.
a) Explicit class import:Ex :- import java.util.ArrayList;
Recommended to use because it improves readability of the code . Best
suitable for Hitech City where readability is important.
b) Implicit class import :Ex :- import java.util.*;
Not recommended to use because it reduces readability of the code. Best
suitable for Ameerpet where typing is important.
Case 2:Which of the following import statement is meaningful ?
a) import java.util.ArrayList;
b) mport java.util.ArrayList.*;
c) import java.util;
d) import java.util.*;
Answer :- a and d
Case 3:-Consider the following code :
Class MyObject extends java.util.rmi.UniCastRemoteObject
{}
The code compiles fine even though we are not working import statement
because we are using fully qualified name.
Note :- Whenever we are not writing fully qualified name it is not required to
use import statement .Similarly, whenever we are writing import statement
then it is not required to use fully qualified name.
Case 4:-

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.

You might also like