Structure of writing java program:
There are total 9 parts of Java program:
- Comments
- package declaration
- import statements
- class declaration
- variable declaration
- constructor implementation
- method implementation
- static blocks
- non-static blocks
1. Comments:
- Java supports 3 types of comments
- i) Single line comment // description about variables
- ii) Multi line comment
/*
we can write
multiple lines of comment
on below method (or) we can put unused logic in multi line comment
for( int i=0; i<20; i++) {
if(i%2==0) {
System.out.println("This is even number");
} else {
System.out.println("This is odd number");
}
}
*/
3. Java Documentation comment
/**
* Author: Tech channel with dsk
* Date: 18-03-2020
* The purpose of this program is show structure of
* writing java program, in which we will declare a class
* in a package, we will also declare private, default,
* protected and public variables and will see their
* accessing scope.
*/
javadoc -d . *.java
Packages and import statements:
- Classes written in one directory if want to be used other directories. Then every class in a directory must have package declaration. Class should be public. Variables, constructors and methods can be declared with desired scope. Such classes can be imported anywhere in the system.
- package declaration is meant/mandatory if any user defined java class want to be reusable within and outside the directory (through out the system). Along with package declaration the class also must be declared as public.
Current directory is
C:\CoreJava\am3\am3\javalang\structure
// A.java
package am3.am3.javalang.structure;
public class A {
private int a; // private access specifier variable
int b; // default access specifier variable
protected int c; // protected access specifier variable
public int d; // public access specifier variable
public void print() {
System.out.println(a); // Yes
System.out.println(b); // Yes
System.out.println(c); // Yes
System.out.println(d); // Y es
}
}
- After declaring package declaration and class as public, the package parent directory must be updated in CLASSPATH. Otherwise package and class cannot be imported in other directories.
CLASSPATH=%CLASSPATH%;C:\CoreJava
import am3.am3.javalang.structure.A;
I will one write sub class of class A within the same package.
C:\CoreJava\am3\am3\javalang\structure
// B.java
package am3.am3.javalang.structure;
public class B extends A {
public void print() {
System.out.println(a); // No
System.out.println(b); // Yes
System.out.println(c); // Yes
System.out.println(d); // Yes
}
}
- I will write one more class in which i will instantiate class A and access its variables.
C:\CoreJava\am3\am3\javalang\structure
// C.java
package am3.am3.javalang.structure;
public class C {
public void print() {
A a=new A();
System.out.println(a.a); // No
System.out.println(a.b); // Yes
System.out.println(a.c); // Yes
System.out.println(a.d); // Yes
}
}
/* D.java within the same directory, but no package declaration tries to access class A variables.
*/
public class D {
public void print(){
A a=new A();
System.out.println(a.a);// na
System.out.println(a.b);// na
System.out.println(a.c);// na
System.out.println(a.d);// a
}
}
// E:\active\core\am3\javalang\first\
/*
E.java sub class of class A outside the package
*/
import am3.am3.javalang.structure.A;
public class E extends A {
public void print(){
System.out.println(a);// na
System.out.println(b);// na
System.out.println(c);// a
System.out.println(d);// a
}
}
/*
F.java instantiated class outside the package
*/
import am3.am3.javalang.structure.A;
public class F {
public void print(){
A a=new A();
System.out.println(a.a);// na
System.out.println(a.b);// na
System.out.println(a.c);// na
System.out.println(a.d);// a
}
}
Access specifiers and their scopes:
- Variable declared as private can be accessible only within the class.
- Default access specifier member can be accessible in both sub class and instantiated class within the package.
- protected member variable can be accessible anywhere within the package and only upto sub class outside the package.
- public member variables can be accessible in both sub classes and instantiated within and outside the package.
Note: package declaration:
- class containing directory or its parent.sub dir can be declared as package. But whichever directory is declared as package, the classpath must be setuped up to its parent directory.
- once package parent directory is updated in classpath we can import package and its classes accessible anywhere in other folders. The recommendation is while importing package class must be imported with fully qualified name (packagename.classname)
- For three .class files only one single A.java file exist in our directory. In future if one more B.java extension file want to be saved in the same directory the file saving is allowed. When B.java file is compiled B.class produces. If that class is a default access specifier class, default access specified .class will come and override .class file of public class.
- Hence every public class must be saved in .java extension file with the same name as class name.
// Class1.java
public class Class1 {
}
public class Class2 {
}
public class Class3 {
}
javac Class1.java
Note: here the first part of the java programming structure Comments and packages are discussed in order to tell how they work
And one more important thing is that this java programming structure is very much important in java programming languages. And there are more seven types that are discussed in which the access specifiers and modifiers are very much important.
0 Comments