Java Program Contains:
Java Program mainly contains 7 parts:
- Package declaration
- Import Statements
- Class declaration
- Variable declaration
- Constructor declaration
- Method declaration
- Static blocks.
Package Declaration:
Package declaration can be any name.Usually the folder/directory where the class exists is declared as package name.
Example:
Suppose a class placed in path like D:\javaprograms\basics.Here javaprograms is 'Project name' and basics is 'package name'.
package basics;
public class exp
{
System.out.Println("Hello ");
}
Import Statements:
While writing java program we will use classes of various package such as io,Util.etc.Java lang package is by default included in every java program if other than java.lang package classes are used we need to import them using
import java.io.*;
import Util.ArrayList;
Class Declaration:
In class declartion we are using access specifier,access modifier.etc
Syntax:
<access_specifier><access_modifer>class ClassName
ex: public final class AnyClassName
{
.....class body.......
}
In the above declaration access_specifier(public or default),access_modifier(abstract or final) declartion are optional.Class keyword is must and some meaningful class name are must to declare while declaring class.
Variable Declaration:
In Variable declartion we are using access specifier,access modifier,datatype,variable name.
Syntax:
<access_specifier><accecss_modifier> data_type variable_name
Java mainly supports two types of variable declarations
- Class Instance Variable/non-static variables
- Class Variables/Static Variables
Class Instance Variable:
If the variable is declared in the class and want memory for every object of class then it must be declared as normally(non-static variable).
Ex:
int age;
String name;
Above we have declared variable without static is non-static variables.
Static Variables:
If the variable want a single copy of memory for any number of objects created for a class then it must be declared as Static Variable.
Ex:
static double min_bal=1000;
In addition to static and non static variables we can also declare variables as final
Final variable:Once the value is assigned at the time of declaration only.It cannot be reinitialized anywhere in the program.
Ex: static final double min_bal=1000;
Construction Declaration:
In every java class by default one constructor exist. Constructor main purpose is to allocate memory for newly created object.
syntax:
<access_specifier> className(){ }
ex:
public class A
{
public A() // constructor
{
}
public static void main(String args[])
{
//for object creation
}
}
- Constructor by default exist.
- Constructor should not have any return value.like int,double, etc because constructor allocates memory it will not return any value.
- Should have same name of class name.
Method Declaration:
Method has some block of code which executes when it calls.
Syntax:
<access_specifier> <acess_modifier> return_type methood_name(param_type param_name)
{ }
<access_specifier> = Optional
<acess_modifier> = Optional
return_type = Mandatory
methood_name=Mandatory
param_type param_name= Optional
Different types of method combination can be:
- Method without parameter and return type
- Method with parameter and No return type(void is consider as no return value)
- Method with parameter and return type
Method without parameter and return type
Ex:
public void hello()
{
System.out.Println("Hello") ;
}
Method with parameter and No return type
Ex:
public void sum(int empno)
{
System.out.Println("Basic salary:"+basicpay) ;
}
Method with parameter and return type:
Ex:
public double salary(double sal)
{
double yearpackage=sal*12;
return yearpackage;
}
Static Block:
static blocks are also part of class.Static block starts with 'static' keyword,followed by open & close braces.Static block execute before main() method execution.The moment java class is loaded into jvm the static block executes first.
Ex:
static
{
//whatever code is needed foe initialization will come here.
}
No comments:
Post a Comment