Main Content RSS FeedRecent Articles

Define Static Keyword »

static is a keyword and means ‘ can be accessed without the help of an object ‘.

static is used an access modifier with members ( variables and methods ) of class. A static member belongs to the whole class. That is, all the objects of the class share ( or access ) the same static variable. Or to say, a static variable is common to all the objects of the class. static variables do not have “this” reference. ‘ this ‘ is a keyword used to refer to a particular current object. This is quite contrast with instance variables where each variable keeps a separate copy of data when called with an object. The static variables like instance variables are given default values ( like zero for int ), if not assiganed any values.

Declaring static members :

static int count ;

static int min( int a, int b)

{

//body

}

Example :

public class StaticDemo {
static int salary = 5000;
public static void display()
{
System.out.println(”Hello”);
}
public static void main(String args[])
{
System.out.println(salary);

//called directly without object
StaticDemo sd= new StaticDemo();

//called with object
System.out.println(sd.salary);

//called with class name
System.out.println(StaticDemo.salary);

//all the below display calls prints Hello

display();
sd.display();
StaticDemo.display();
}
}

Output :

double d =10.5 ;
int x=d;
int y=(int)d ;
System.out.println(y);

In the above, casting is done explicitly by us as we are assigning d of datatype double ( 8 bytes ) to x of datatype int ( 4 bytes ).

What is Abstract Class?Explan »

Abstract Class  :

In some situations we want to define a superclass that declares the structure of a given abstract without providing a complete implementation of every method. That means we only provide general code and it is developers responsibility to customize that class. To make sure this, we can make the abstract, that means the developer will have to override your method .

The abstract keyword is used to make a method abstract if we have an abstract in a class, we also have to make the class an abstract.

Note :

–> An abstract class is a class that contain one or more abstract methods

–> An abstract class can have instance variables, concrete methods in addition to abstract methods

–> We cannot create object to abstract class

–> We can create reference variables to abstract class

–> All the abstract methods of abstract class should be implemented in its subclass.

–> A class cannot be both abstract and final.

–> Abstract class reference cannot refer to individual methods of the subclasses

Explain Interface?Define »

Interface:

In Java language, an interface is like a stripped-down class; it specifies a set of methods that an instance must handle, but it omits inheritance relations and method implementations.

Object-oriented programming is sometimes modeled as communication between objects like one object talks to, or sends a message to, another object ( by invoking a method on it ). An object needs to know the order to talk with another object ; a set of abstract methods i.e., method minus implementation information. Put the another way, an interface specifies how we can talk to an object,but says nothing about what kind of object will handle our messages. It is job of one or more class instances to provide the substance behind an interface’s promise.

The general form of an interface definition is :

interface name

{

variable declarations;

method declarations;

}

Here, interface is the keyword and name is any valid Java variable ( just like class names ).

Variables are declared as follows:

public static final type variableName = value;

Methods declaration will contain only a list of methods without any body statements.

Example:

return-type methodName(parameter_list);

The following is an example of an interface definition that contains two variables and one method :

interface Item

{

public static final int prodCode = 1001;

public static final String prodName = “Fan”;

public abstract void display();

}

Here the code for the method is not included in the interface and the method declaration simply ends with a semicolon. The class that implements this interface must define the code for the method.

NOTE:

–> All the Interface methods are Abstract only by Default.

–> we can’t create an object to the interface. But we can create a reference variable to interface.

–> Once the interface is provided we can create ‘n’ number of implementation classes

–> we can create object to implementation class by using this we can allow all features of the super class.

Define Array in Java »

An Array is a group of similar data type items that share a common name. Arrays occupies contiguous memory locations. Array size is fixed and cannot be altered dynamically. For example, we can define an array name as salary to represent a set of salaries of a group of employees. A particular value is indicated by writing a number called index number or subscript in brackets after the array name.

For example:

salary[3]

Represents the salary of 4th employee(array index starts from zero ). While the complete set of values are referred to as an array, the individual values are called elements. Arrays can be of any primitive data type.

One-dimensional Arrays :


A list of item can be given one variable name using only one subscript and such a variable is called as one-dimensional array.

Creating an Array :


Arrays must be declared and created in the computer memory before they are used. Creation of an array involves three steps;

(i) Declaring the array

(ii) Creating memory locations

(iii) Putting values in the memory locations

Declaration of Arrays :


Arrays in java may be declared in two forms :

1st way : datatype arrayname[];


2nd way : datatype[] arrayname;


Examples :


int number[];

float average[];

int[] counter;

float[] marks;


Note: The size of the array should not be mentioned while declaration


Creation of Arrays :


After declaring an array, memory should be allocated. Java allows the creation of arrays using new keyword, as shown below:

type arrayname=new type[size];


Examples:


int number[];

number=new int[5];

float average[];

average=new float[10];


This lines create necessary memory locations for the arrays number and average and designate them as int and float respectively. Now, the variable number refers to an array of 5 integers and average refers to an array of 10 floating point values.

It is also possible to combine both the steps– declaration and creation into aone as shown below.

float average = new float[10];

int num [] = new int[5];


Sample example of array’s in java are as follows;


//java program for Total marks and percentage.


import java.io.*;
class Arr
{
public static void main(String args[])throws IOException
{
//to accept data from keyboard

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println(”How many Subjects”);
int n=Integer.parseInt(br.readLine());


//create the array with size n
int marks[] = new int[n];

//store marks into an array marks[]
for(i=0;i<n;i++)
{
System.out.println(”enter marks”);
marks[i]=Integer.parseInt(br.readLine());
}

//display the marks again and final total
int tot=0;
for(int i=0;i<n;i++)
{
System.out.println(marks[i]);
tot=tot+marks[i];
}
System.out.println(”total marks=”+tot);

//final percentage of marks

float percent=(float)total/n;

System.out.println(”percentage=”+percent);
System.out.println(”arr size=”mark.length);
}
}