java basics
A Java program is mostly a collection of objects talking to other
objects by invoking each other's methods. Every object is of certain
type, and that type is defined by a class or an interface. Most Java
programs use a collection of objects of many different types.
class:
template describes the kind of state and behavior that objects of its type support.
Real time examples:
1)
public class Dog{
String breed;
int age;
String color;
void barking(){
//barking action
}
void hungry(){
//hungry action
}
void sleeping(){
//sleeping action
}
}
A class can contain any of the following variable types.
- Local variables . variables defined inside methods,
constructors or blocks are called local variables. The variable will be
declared and initialized within the method and the variable will be
destroyed when the method has completed.
- Instance variables . Instance variables are variables
within a class but outside any method. These variables are instantiated
when the class is loaded. Instance variables can be accessed from inside
any method, constructor or blocks of that particular class.
- Class variables . Class variables are variables declared with in a class, outside any method, with the static keyword.
A class can have any number of methods to access the value of various
kind of methods. In the above example, barking(), hungry() and
sleeping() are methods.
Object:
At runtime when the Java virtual machine encounters new key will, it
will use the appropriate class to make an object which is an instance of
that class. That objects will have its own states and have access to
all of the behaviors defined in the class from which those created.
State ( instance variable ):
Each object ( instance of a class) will have its own unique set of
instance variables as defined in the class. The values assigned to an
objects instance variables makes up the objects state.
behavior( methods ):
Methods are where the class logic's are stored . They are where the real work gets done and data gets manipulated.
Now i can give a Real time example for Class, Object, and Object's state & behavior
If we take B.Tech graduation there are different classes exist like CSE, EEE, ECE, MEC
and so on. suppose if we take CSE class for our class example. all the students who
graduated in CSE will have different percentage of knowledge in all the subjects of CSE
and they can do software job, any govt job after completion of their course.
here
i)CSE class considered as an example for class.
i) Every student will act as an object.
ii) Percentage of knowledge in subjects is the state of those student objects
iii)Jobs they can do will act as behavior. In performing their job their subjects knowledge get manipulated.
Some important topics need to be discussed those are
Constructors:
When discussing about classes one of the most important sub topic
would be constructors. Every class has a constructor. If we do not
explicitly write a constructor for a class the java compiler builds a
default constructor for that class.
Each time a new object is created at least one constructor will be
invoked. The main rule of constructors is that they should have the same
name as the class. A class can have more than one constructor.
Example of a constructor is given below:
public class Employee{
public Employee(){
//code follows
}
public Employee(String designation){
// code follows
}
}
A constructor is a block of code that is
called when a new object is created. In the constructor there is
typically code to initialize the member variables of the object or to do
other things that need to be done when a new object is created.
Creating an Object:
there are mainly three steps involved in creation of java object
those are
1 . loading
2.Instantiating
3.Intializing
Assume that we have a class with name "student" now we want to create object for it
the statement that will write is
st1: student std;
st2 : std=new student();
in statement one it will cretae a reference with std in the stack and its not the object
in statement 2 when u say actual process starts :
LOADING :
As the student class will exist in the hard disk (secondary storage) it will be loaded into the main memory(RAM)
INSTANTIATING:
Once the loading is done it will allocate the memory for the non static members of the class in the heap.
INITIALIZING:
Once the memory is allocated then the data members will be intialized with default values
provided by JVM.
after completing these 3 steps the address of the object will be stored in the a hash table
along with index . this index will be stored in the reference varaible .
Example:
This example explains how to access instance variables and methods of a class:
public class Puppy{
int puppyAge;
public Puppy(String name){
// This constructor has one parameter, name.
System.out.println("Passed Name is :" + name );
}
public void setAge( int age ){
puppyAge = age;
}
public int getAge( ){
System.out.println("Puppy's age is :" + puppyAge );
return puppyAge;
}
public static void main(String []args){
/* Object creation */
Puppy myPuppy = new Puppy( "tommy" );
/* Call class method to set puppy's age */
myPuppy.setAge( 2 );
/* Call another class method to get puppy's age */
myPuppy.getAge( );
/* You can access instance variable as follows as well */
System.out.println("Variable Value :" + myPuppy.puppyAge );
}
}
If we compile and run the above program then it would produce following result:
Passed Name is :tommy
Puppy's age is :2
Variable Value :2
Source file declaration rules:
As the last part of this section lets us now look into the source
file declaration rules. These rules are essential when declaring
classes, import statements and package statements in a source file.
- There can be only one public class per source file.
- A source file can have multiple non public classes.
- The public class name should be the name of the source file as well which should be appended by .java at the end. For example : The class name is . public class Employee{} Then the source file should be as Employee.java.
- If the class is defined inside a package, then the package statement should be the first statement in the source file.
- If import statements are present then they must be written
between the package statement and the class declaration. If there are no
package statements then the import statement should be the first line
in the source file.
- Import and package statements will imply to all the classes
present in the source file. It is not possible to declare different
import and/or package statements to different classes in the source
file.