Declaring Classes
class Myclass{// field,constructor and method declaration
}
This is a class declaration. The classes body (the area between the braces) contain all the data that are related to life cycle of the object.constructor for initializing new objects,fields provides state of class and it's object and methods provide behavior of the class and it's object.
Below component are basically use for classes declaration
- Modifier such as private,default,protected and public ( Modifier in java specifies accessibility-scope)
- The class name ,with the initial letter capitalized by convention.(Myclass)
- The extends key word use to create sub class (Child) from a java super class (Parent).A class can only(sub class) extends one parent.
- The implements key word use to implement interface.A class can implement more than one interface
- The class body,surrounded by the braces,{}.
Declaring Variables
There are several kind of variable
- Member variables in class - These are called fields
- Variable in Methods or Block-These are called local variable
- Variables in Method declaration -These are called parameters
public class Employee{
private String name; // This instance variable -Member variable
public void employeeAge(int empAge){ //This is parameter
int age=18; // This local variable
}
}
.
Three components used in the field declaration.
- Zero or more modifier such as (public,private and etc..)
- The field's type
- The field's name
Access Modifiers
Access modifiers in java specifies accessibility of the field's.We will discuss this on later.
- public modifier -the field's accessible by all class
- private modifier-the field's accessible by withing it's own class
Types
All variable must have a type.You can use primitive type such as int,float,boolean etc..as well as you can use reference types array,String and Object.
Variable Names
The names of variable in the java language are referred to as identifier.
- All the variable names must begin with the letter of alphabet
- You can't used java key word as a variable name
Defining Methods
Here is the example of typical method declaration.
public void employeeAge(int empAge) throws Exception{
//method body
}
More generally, There are six components in the method declaration
More generally, There are six components in the method declaration
- Modifier such as private,public and etc..
- The return type-The data type of the value returned by method or void the method doesn't have return value
- Method name- The rules for field's name are apply as well as method's name
- Parameter list in parenthesis
- An exception list
- Method body enclose between braces
Constructors
A class contains constructor that are invoke to create object from the class blueprint.Constructor declaration look like method declaration --except that use the name of class name and have no return type.
public class Employee{
public String name;
public Employee(){ // constructor 1
name="test name";
}
public Employee(String name){ //constructor 2
name=name;
}
}
Both constructor could have been declared in Employee because they have different argument list.If you don't provide any constructor in class,The compiler automatically provides a no argument,default constructor for any classes without constructor.
you can use access modifier in a constructor's
public class Employee{
public String name;
public Employee(){ // constructor 1
name="test name";
}
public Employee(String name){ //constructor 2
name=name;
}
}
Both constructor could have been declared in Employee because they have different argument list.If you don't provide any constructor in class,The compiler automatically provides a no argument,default constructor for any classes without constructor.
you can use access modifier in a constructor's