Tuesday, 22 November 2016

Initializing a blank final variable in Java

How to initialize a blank final variable in Java?

Example Java Program to demonstrate the initialization of blank final variable using Constructor

                  A field can be declared as final. Doing so prevents its contents from being modified, A final variable that is not initialized at the time of declaration is known as blank final variable. The blank final variable can be only initialized using its constructor method.

                  As per the coding convention, it is common to use all upper case letters to represent a final variable. 


                    final in REGNO = 15480;


//Java Program to demonstrate the initialization of blank final variable using Constructor
class FinalInitialize
     final int VALUE;
     FinalInitialize(){       // Constructor
          VALUE=10;           // Initialized final variable inside constructor
          System.out.println("Final Value is : "+VALUE);
public class InitializeFinal {
     public static void main(String[] args) {
          new FinalInitialize();

               In the above program, The FinalInitialize() constructor initiate the value of the final variable VALUE.

Sunday, 13 November 2016

Java Program to find the Volume of a Box

Volume of a Box in Java

                  The below program demonstrates simple class creation and perform volume calculation of a box.


class Box {
double width;
double height;
double depth;
public class BoxJavaProgram {
    public static void main(String[] args){
        Box mybox = new Box();
        double vol;
        mybox.width = 20;
        mybox.height = 30;
        mybox.depth = 10;
        vol = mybox.width * mybox.height * mybox.depth;
        System.out.println("Volume is " + vol);


Volume is 6000.0

Thursday, 10 November 2016

Difference between Instance and Object in Java

Difference between class object and class instance in java

               The new operator instantiates a class by allocating memory for a new object and returning a reference to that memory. The phrase "instantiating a class" means the same thing as "creating an object." When you create an object, you are creating an "instance" of a class, therefore "instantiating" a class.

Object and Instance in Java
Difference between object and instance in Java

It is obvious to get confused by the words class object and class instance. A class is a template for an object and an object is an instance of a class. To make it clear, we can also say that the instance of a class is object.

There is no need of differentiating the words object and instance as both doesn’t make any difference when is used interchangeably.

For Example:

class Animal

Object of class Animal and instance of class Animal are same. When you create an instance of the class animal, the instance is an object and the type of that object is “class Animal”.

Sunday, 2 October 2016

Javascript Date

The Javascript Date object lets you work with dates (years, months, days, hours, minutes, seconds, and milliseconds)

Simply for displaying dates its like below:
document.getElementById("demo").innerHTML = Date();

The important methods of date object are as follows:

getFullYear()returns the year in 4 digit e.g. 2015. It is a new method and suggested than getYear() which is now deprecated.
getMonth()returns the month in 2 digit from 1 to 31.
getDate()returns the date in 1 or 2 digit from 1 to 31.
getDay()returns the day of week in 1 digit from 0 to 6.
getHours()returns all the elements having the given name value.
getMinutes()returns all the elements having the given class name.
getSeconds()returns all the elements having the given class name.
getMilliseconds()returns all the elements having the given tag name.

Current date example:
var today=new Date();

Current Time example:
var today=new Date();
var h=today.getHours();
var m=today.getMinutes();
var s=today.getSeconds();