Showing posts with label modifiers. Show all posts
Showing posts with label modifiers. Show all posts

Wednesday, 18 June 2014

Java Classes And Objects

In object-oriented programming technique, we design a program using objects and classes.
Object is the physical as well as logical entity whereas class is the logical entity only.

Object in Java:

Object is the real world entity that has state and behavior.
e.g. laptop, bike, dog , pen, table, car etc. It can be physical or logical.

 Car is an object. Its name is Honda City, color is Blue etc. known as its state. 
Object is an instance of a class. Class is a template or blueprint from which objects are created. So object is the instance(result) of a class.

Introduction to Java Classes:

A class is nothing but a blueprint or a template for creating different objects which defines its properties and behaviors.
A class can contain fields and methods to describe the behavior of an object.
Methods are nothing but members of a class that provide a service for an object or perform some business logic. 
Java fields and member functions names are case sensitive.
Current states of a class’s corresponding object are stored in the object’s instance variables.
Methods define the operations that can be performed in java programming.

A class has the following general syntax:

class ClassName
{
         //class body
}

Example of class

public class Rectangle
{
     int length;
     int breadth;
     int area;

     public int getArea()
     {
       area=length*breadth;
       return area;
     }
}

A class in java can contain:

data member
method
constructor
block
class and interface

How do you reference a data member/method ?
This is done by stating the name of the object reference, followed by a period (dot), followed by the name of the member inside the object.

For Example:
RectangleObject.length=20;
RectangleObject.breadth=10;
RectangleObject.getArea();

Class Variables(Static Variables)
We use class variables also know as Static variables when we want to share characteristics across all objects within a class. 
When you declare a variable as static only single copy is created among all the objects.
Hence when one object changes the value of a class variable, it affects all objects of the class. 
We can access a class variable by using the name of the class.
Static variables can be accessed even though no objects of that class exist. It is declared using static keyword.

Class Methods(Static Methods)
Class methods, similar to Class variables can be invoked without having an instance of the class.
You cannot call non-static methods from inside a static method.

Instance Variables
Each Object would have its own copy of the variable. 
When you change the value of the instance variables , it does not affects the other objects.

Top level class modifier
The modifier applicable to top level class are:
1. public
2. <default>
3. abstract
4. final

How to create Object of a class ?
By new key word we can create object of a class.New keyword occupy memory for the objects.
public class Area
{
   int length;
   int breadth;
   int area;
   public int area()
   {
      area=length*breadth;
      return area;
   }
   public static void main(String[] args)
   {
        Area a=new Area(); //object creation
        a.length=20;
        a.breadth=10;
        System.out.println("Area = " + a.area());
   }
}
OUTPUT
Area = 200



Sunday, 18 May 2014

Basics OF Java

It  is very important to keep in mind the basic syntax of java program.
Basic Syntax:

Case Sensitivity - Java is case sensitive, which means identifier Test and test would have different meaning in Java.

Class Names - Class names should be start with upper case letter.If several words are used to form a name of the class, each inner word's first letter should be in Upper Case.
E.g. class MyFirstJavaProgram

Method Names - All method names  should be start with a Lower Case letter.If several words are used to form the name of the method, then each inner word's first letter should be in Upper Case.
E.g public void myFirstMethod()

Source  File Name - Name of the program file should exactly match the class name.
When saving the file, you should save it using the class name  and append '.java' to the end of the name.If the file name and the class name do not match your program will not compile.
E.g. : Assume 'Test' is the class name. Then the file should be saved as 'Test.java'

public static void main(String args[]) - Java program execution  starts from the main() method which is a mandatory part of every Java program.The main() method should have public and static  access modifier,void return type and string array as argument.
If we supply return type other than void then program compile successfully but it's not execute.

Java Identifiers:
All Java components require names. Names used for classes, variables and methods are called identifiers.
In Java, there are several points to remember about identifiers. They are as follows:
1. All identifiers should contains only character(A to Z or a to z),digit (0 to 9) and ( $ , _ ).
2. All identifiers should begin with a letter (A to Z or a to z), dollor character ($) or an underscore ( _ ).
3. After the first character identifiers can have any combination of characters,digits and ($) ,( _ ).
4. A key word cannot be used as an identifier.
5. Identifiers are case sensitive.

Example of identifiers
Legal identifiers: age, $salary,  _value , __1_value , abc123,abc_ xyz
Illegal identifiers: 123abc, -salary,@abc,abc!,abc@xyz

Java Modifiers:
There are two types of modifiers in java:
1. Access Modifiers: default, public , protected, private
2. Non-access Modifiers: final, abstract, strictfp,transient,native .
We will see into more details about modifiers in other post.

Java Variables:
We would see following type of variables in Java:
1.Local Variables
2.Class Variables (Static Variables)
3.Instance Variables (Non-static variables)
Class and Instance variables are global variables.

Java Keywords:
The following list shows the reserved words in Java. These reserved words may not be used as constant or variable or any other identifier names.

abstract assert byte boolean
break case catch char
class const continue default
do double else enum
extends final finally float
for goto if implements
import instanceof int interface
long native new package
private protected public return
short static strictfp super
switch synchronized this throw
throws transient try void
volatile while
Comments in Java:
Java supports single-line and multi-line comments . All statements  available inside any comment are ignored by Java compiler.

public class Test
{
public static void main(String []args)
{ // This is an example of single line comment /* This is also an example of multi line comment. This is also an example of multi line comment. This is also an example of multi line comment This is also an example of multi line comment */
       System.out.println("Hello Collegeone users !"); }
}