Sei sulla pagina 1di 9

A java program [java source file] can contain any number of classes, any number of main methods but at

most one class can be declared as public.

 If there is no public class then we can used any name for java program.
 If there is a public class, compulsory named of the program under name of public class must be
matched.
 If a class A contains another class B, then two class file generated, one is A.class and another is A&B ,
Where A.class have no B class info and A&B class have both A and B all info.
 In cmd if you write file name at any cases and Public class name is ABC then file name written as aBc
etc then we compile this only by ABC.

 Whenever we compiling a dot java program, for every class, interface present in the program, a
separated dot class file will be generated.
 While executing/run a java class, main method of corresponding class will be executed.
 If the corresponding class does not contain main method then we will get error message saying main
method not found.
 If the corresponding dot class files not available then we are going to get an error.

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Import statements are required, if you are trying to use classes of other packages.

The highest advantages of import statement are we are not required to use fully qualified named. Thus length of
code reduced and code readability improved.

Two types Import Statements are there, one is Explicit and another one? Yes, Implicit. All IDE’S by default
automatically used explicit class import.

java.util.ArrayList A= new ArrayList(); // but not recommended as no readability

Import com.bank1.*;
// Implicit but not recommended as sub package classes/interfaces not available
Import com.bank2.Account;
// Explicit import highly recommended & easily says account class belongs to bank2 package
Import com.bank3.*;
Import com.bank4.Employee.Salary;
Public class A
{
Public static void main(String args[])
{
Account A=new Account A();
Loan B=new Loan B(); // Cannot say loan class exist in bank1 or bank3
Salary s=new Salary(); // works as sub package level imported
}
}
After importing, number of times short name can be used. If you want to use short name directly, compulsory
top import statement must be required. Even if you are using 100 classes from same package, compulsory we
should go for explicit import only as readability increases and highly recommended.

Two packages not need to import as by default available for every java program.

1) java.lang packages

String S=new String();


Exception E= new Exception();
Thread T =new Thread();

2) Default Packages
- CurrentWorkingDirectory (CWD)
- All Classes, Interfaces present inside cwd, by default available to program & not required to import
any.

** Two different classes cannot have same object name in same program.

** In same package , 2 java files cannot have same class name

** Text.java Student.java two files are in same folder/same directory/same packages, so they can communicate
with each other without the need of import statement.
Pattern class file exist in regex packages which is sub package of util. How we import & access this pattern
class?
Import.java.*; // All classes & Interfaces present in java package by default available but not sub package classes.
Import.java.util.*;// still cannot use Pattern class as sub package level not reached.
Import.java.util.regex.*; // Yes, Pattern class can be used as sub package regex reached.

** If you want to use sub package level classes & interfaces compulsory we have to write import
statement until sub package level.

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Package is an encapsulation/grouping mechanism, which grouping related items/things/classes/interfaces are
into a single unit.

 java.sql package contains all java classes and interfaces which are related to database operations.
 Two date classes in java. One is presented in SQL package and another in Util package.
java.sql.date
java.util.date

 To resolve naming conflicts or unique identification like date class


 Modularity of application improved [Grouping related things into one group]
 Maintainability
 Security [Outside person cannot access non-public class of an package]

Highly recommended, good practice but almost mandatory to use package statement in real time.
Package naming: reverse of client domain.
com.brac.account as domain name is unique.
In any Java source file / program at most one package statement allowed means 0 or 1.
Package statement should be declared before import statement. In java program fist non-comment
statement is package.

Compile package
1. javac test.java generate class file in cwd, but its meaningless for package as want to generate class file
in com then brac,account folder.
2. javac –d . test.java now test.class file generated inside account folder even folders not available then
javac automatically create folders.
3. javac –d E: test.java to change directory, .dot means current directory of code file we write.
4. dir text.class to check directory
Run package
1. Compulsory to used fully qualified name
2. Java com.brac.account.test
////////////////////////////////////////////////////////////////////////////////////////////////////

System.out.prinln not works without any method in a class.

Class A
{
System.out.prinln(); // Error out not fount
Void msg()
{
System.out.prinln();
}
}

Public class B
{
Public static void main(String[] args)
{
System.out.prinln();
}
}
1. Java Source File can be named by anything if not public class exists.
2. If public class exists, then source file name must be same as public class name.
3. JVM creates separate class file for every classes. But no class will generated for filename.
3.1. If any class contain no main method and try to run then CE
3.2. If two classes [public non-public not matter] named are differenced only by cases like ABc abC then
only one class file generated from top most class of these two classes. Here ABc.class file generated
but you cannot run ABc.class only you can run abC.class as this replace ABc.class file.
Normally in computer we cannot save 2 file as abc.txt and AbC.txt
4. From CMD, if file name is ABc.java then in cmd you can write this in any cases you want like abc.java
ABC.java

Potrebbero piacerti anche