vivarem logo
/ Vivarem / Computers and IT / IT Education / online / tutorial / Java / OOP / 1 /
There are 0 sub sections

You can add Your Link here with New Link
Java Object Oriented Programming -1

Java Class
A class is the most basic programming unit in java. it may contain member variables, functions and constructors.A class encapsulates the data members and functions using access controll mechanisms. Functions processes data members of a class The Naming conventions used in java is always capital letters are used to start first letter of class name and any subsequent words.small letters used as first letter for objects, variables and functionsand subsequent words starts with capital letter without any space. keywords cannot have special characters including space

Example class


//This is an oneline comment
/** a multiline comment
the first line defines the package name
**/
package com.vivarem.examples;

/** you may sepcify the package that you want to imported into your application. ie those files that you are depended for successful compilation of project
The imported classes belong to package java.lang and it is never used in application itself.* is used for importing all the classes in a package
**/
import java.lang.*;
/**a class can be public or default.you should use public if you have a main function within the class
The next line defines a class named Simple Class all the statements of class are enclosed within open and close braces (couble brackets) as {} block eg
public class MyClass{
}
**/
public class SimpleClass{
/** int a,b,sum are variables or data members **/
int a,b,sum;
/** next is a sepcial function called constructor used for initializing objects The constructor can have any four access methods namely public, protected, private and default.
it has 2 paramenters passed to it. There is a defualt constructor with no parameters. The 'this' keyword says the variable accessed is a data memeber of the object and not a local variable. all member function parameters zre local variables.
**/
public SimpleClass(int a,int b){
this.a=a;
this.b=b;
}

/** sum ia member function with public access specifier return an integer
while public int specifies what is the return type, the return statementactually returns the value.**/
public int sum(){
sum=a+b;
return a+b;
}
/** sum() calls the sum function the void declars the function statement to return nothing hence if you use void there is no need to specify the return in the function block
System.out.println outputs the string to the console
**/
public void show(){
int sum=sum();
System.out.println("local sum="+sum);
System.out.println("object sum="+this.sum);
}
/** public static void main is special method which makes any class defined with public access sepcifier an application you can supply any no of arguments to the application which is accessible through args[], which is an array of variables.
**/
public static void main(String args[]){
SimpleClass obj= new SimpleClass(10,20); //creates an object obj and initailises the variables with 10,20 values for a,b respectively
obj.show();//calls the show function of the object obj
System.out.println(args[0]); //will print first argument passed to the java application
}
}


Eventhough eclipse or bluej is recommeded as an ide, if you are not having either you may also use command prompt for compiling and running a java program.

Compiling and Running Java Programs
To run the above program save the program as SimpleClass.java in directory ./src/com/vivarem/examples. create another folder ./class go to src directory and type the following to compile
/src>javac -d ../class com/vivarem/examples/SimpleClass.java
where javac invokes the java compiler -d directs the complier to write the generated class files in the class sub directory of parent directory and com/vivarem/examples/SimpleClass.java is actual path to file SimpleClass.java your output will be stored as class/com/vivarem/examples/SimpleClass.class
to run the above class at src directory type
src> java -classpath ../class:$CLASSPATH com.vivarem.examples.SimpleClass hello hello2 hello3
giving the following output
local sum=30
object sum=30
hello
Content |
Primitive Types and Strings and operators >


You can Add a Classified here with New Classified