Class, Object and Method

Java; Class, Object and Method

What is a class? How to Define a class?

class is a user defined data type with template that serves to define its properties.

class classname [extends superclassname]
{
[ variable declaration; ]
[ methods declaration; ]
}

Here, class is a keyword, classname and superclassname are any valid Java identifiers.
The Keyword extends indicates the properties of the superclassname class extended to the classname class this concept is known as inheritance.

Adding variables and methods to the class
class Rectangle
{
// these are called variable declaration
int a;
int b;
// this is method declaration
// syntax of method declaration

type methodname (parameter-list)
{

method-body;
}
type specifies return type of method it could be any data type.
method name specifies the name of the method
parameter-list contains variable names and types of all the values we want to give to the method as input.The variable in the list are seperated by commas.

Example : void getdata( int x , int y )
{
a = x ;
b = y ;
}
}

A First Simple Program

/*
This is a simple Java program.Call this file “Example.java”.
*/

class Example {

// Your program begins with a call to main().

public static void main(String args[]) {
System.out.println(“This is a simple Java program.”);
}
}
The first thing that you must learn about Java is that the name you give to a source file is very important. For this example, the name of the source file should be Example.java. Let’s see why.

In Java, a source file is officially called a compilation unit. The Java compiler requires that a source file use the .java filename extension.

Compiling the Program

To compile the Example program, as shown here:

C:\>javac Example.java

The javac compiler creates a file called Example.class that contains the bytecode version

of the program..

To actually run the program, you must use the Java interpreter, called java as shown here:

C:\>java Example

When the program is run, the following output is displayed:

This is a simple Java program.

A Closer Look at the First Sample Program

Although Example.java is quite short. Let’s closely examine each part of the program.

The program begins with the following lines:

/*
This is a simple Java program.Call this file “Example.java”.
*/

multiline comment. This type of comment must begin with /* and end with */.
Anything between these two comment symbols is ignored by the compiler.

single-line comment, shown here:

// Your program begins with a call to main().
This is the second type of comment supported by Java. A single-line comment begins

public static void main(String args[]) {

main( ) method

All Java applications begin execution by calling main(

public keyword is an access specifier, which When a class member is preceded by public, then that member may be accessed by code outside the class in which it is declared.

static allows main( ) to be called without having to instantiate a particular instance of the class. This is necessary since main( ) is called by the Java interpreter before any objects are made.

void simply tells the compiler that main( ) does not return a value. As you will see, methods may also return values.

String args[ ] declares a parameter named args, which is an array of instances of the class String. In this case, args receives any command-line arguments present when the program is executed.




Welcome Back!

Login to your account below

Retrieve your password

Please enter your username or email address to reset your password.