Tuesday, 11 August 2015

Difference between Methods and Constructors in Java

Methods Vs Constructors in Java

What is the difference between Methods and Constructors in Java?

               It is obvious that Constructor and Method in Java make confusion for the beginners. Here are few points which illustrate the difference between a constructor and method in Java.





It is not necessary to use the same name of the class to create a Method.

The name of the Constructor should be same as the class name it resides.


Method is an ordinary member function which is used to expose  the behavior of an object

Constructor is a member function of a class used to initialize the state of an object


Methods must have return type unless it is specified as void.

Constructor does not have a return type.


Compiler does not create a method in any case if one is not available.

Java compiler creates default constructor if the program doesn’t have one.


Methods invoked explicitly. 
Invoked using the dot operator.

Constructor invoked implicitly. i. e. Invoked using the keyword ‘new’.

               Hoping that the above table gives an idea about how Constructors are different from methods in Java.

Constructor Method Difference

Tuesday, 4 August 2015

Static keyword in java

Java Static keyword

For memory management in java used the static keyword.
We can use this keyword with variable,method,blocks and nested class.

In Java Variables can be declared with the “static” keyword.

Example: static int x = 0;  
  • It is a variable which belongs to the class and not to object(instance)
  • Static variables are initialized only once , at the start of the execution . These variables will be initialized first, before the initialization of any instance variable.
  • A single copy to be shared by all instances of the class
  • A static variable can be accessed directly by the class name and doesn’t need any object
Variables which are declared with a static keyword inside a class (outside any method) are known as Class variable / Static variable. They are known as Class level variable because values of these variables are not specific to any instance but are common to all instances of a class. Such variables will be shared by all instances of an object.

1. Java Static Variable

Sample Program:

// static variable 
class Office{ 
   int id; 
   String name; 
   static String department ="Payroll"; 
   Office(int a,String b){ 
   id = a; 
   name = b; 
 void display (){
 System.out.println(id" "+name+" "+department);
 public static void main(String args[]){ 
 Office ab1 = new Office(101,"Gokul"); 
 Office ab2 = new Office(102,"Ram"); 


101 Gokul Payroll
102 Ram Payroll

2. Java Static method

// Static method

class Office{ 
     int id; 
     String name; 
     static String department = "
     static void change(){ 
     department = "HR"; 
     Office(int a, String b){ 
     id = a; 
     name = b; 
     void display (){System.out.println(id" "+name+" "department);} 
    public static void main(String args[]){ 
    Office ab1 = new Office (101,"Gokul"); 
    Office ab2 = new Office (102,"Ram"); 
    Office ab3 = new Office (103,"Krishna"); 


101 Gokul HR
102 Ram HR
103 Krishna HR

Saturday, 1 August 2015

Naming conventions in java

Java Naming Conventions

What Is a Naming Convention?

          Naming Conventions is nothing but a standard specifically used by the Java professional to represent the name of the class, interface, method, variable, package etc in a Java Program. In other words, Naming Conventions in Java is a standard rule to follow in selecting name of any identifiers in a program.

Naming Convention

          It is not mandatory to follow the Naming Conventions to run a java program. That’s why it is known as convention and not made as a rule. 

Standard Java Naming Conventions

          Standard Java Naming conventions for different identifiers are listed below


          Package names should be in all lowercase letters (small letters). 

Eg: com.packet.example, crack.analyser, name.preview


          Typical class name should be in nouns and represented by using the first letter capital method (Upper Camel Case Method). Use simple and descriptive nouns as Class name.  

Eg: class LogicalMeter, class Calculator, Class SeperateString


          Method name should be verb and use mixed case letters to represent it in programs. Mixed case letters are also knows as lower camel case letters. 

Eg: toPrint(), colorBackground()


          Constants are declared with the help of ‘static’ and ‘final’ keyword in Java. Constant variables should declare with full Upper Case letters with underscore(_) for word separation.

Eg: static final int MAX_WIDTH, static final int MAX_HEIGHT


          Interface name is also named like class name with Upper Camel Case method. 

Eg: interface ActionListener, interface Runnable


         Variable name should be in mixed case ie, naming start with small letter and use capital for internal words. It is easier to use simple one letter variable for temporary usage. Common variables used for storing integers are I, j, k and m. Common variables used for characters are c, d and e.

Eg:  float myHeight, String userName

Advantage of naming conventions in java

          Naming conventions make programs more understandable by making them easier to read. They can also give information about the function of the identifier-for example, whether it's a constant, package, or class-which can be helpful in understanding the code.

Monday, 27 July 2015

Object and Class in java

The Concept of Object and Class in Java

Object in Java

Basically an entity that has state and behavior is known as an object. It has three main characteristics like state,behavior and identity.

Object is an instance of a class.

Object and Class

Class in java

A class is a group of objects that has common properties.

A class can be defined as a template/ blue print that describes the behaviors/states that object of its type support.

Syntax to declare a class

class {  

    data member;  



Sample Program:

class FirstProgram{

   int a;     //data member
   String name;//data member
   public static void main(String args[]){  
      FirstProgram fp=new FirstProgram();  //creating an object of FirstProgram



Note: The Output of the program shows the default values in the variables 'a' and 'name'.