Java Programming Structure

Posted by Kalyan Kurasala  |  No comments

Java, is one of Programming Language, which follows Object-Oriented approach. Before using Java, C++ is most popular Object-Oriented Programming Language. This partially supports OOP concepts. Doesn't support Garbage Collection.It doesn't provide built-in support for threads. It doesn't support the portability etc.

But Java is a purly object-oriented programming language. Before going to know about the structure of Java Program, we should know about Object.

What is Object?
In general Object is a real time entity i.e., object is a real thing. It may be a pen, book, bike, computer or anything.

In Object-Oriented Programming an Object is a collection of memory , that has data fields and procedures. These data fields are also referred as instances and procedures are referred as methods. These Objects are the instances of classes.

What is a Class?
A class is a blueprint from which individual objects are created. A best example for class is as follows:

In the real world, you'll often find many individual objects all of the same kind. For example consider a car. There may be thousands of other cars in existence, all of the same manufacture and model. Each car was manufacture from the same set of blueprints and therefore contains the same components. In object-oriented terms, we say that your car is an instance of the class of objects known as cars. So, a class is the blueprint from which individual objects are created.

In Java a class can be defined by using the keyword class.

A simple Java Program Structure is:

access-specifier class classname {

//defining instance variables
datatype1 instance-variable1,instance-variable2,instance-variable3,...,instance-variablen;
datatype2 instance-variable1,instance-variable2,instance-variable3,...,instance-variablen;
...
datatypen instance-variable1,instance-variable2,instance-variable3,...,instance-variablen;

//defining methods

access-specifier return-type methodname(parameter-list) {

//method() code here
}
...
}

Here,

access-specifier: Java defines 3 types of access-specifiers.
1) public: We can access anywhere in the program.
2) private: We can access only local.
3) protected: It has different access permissions.

By default Java take every class, method and variable as public.

class: You already know class is a keyword to define a class.

classname: It is the name of the class. Which has some rules that is How to name a class?

A class name must not start with a number.
It must start with an alphabet or an underscore.
No special symbols are allowed. Only underscore is allowed.
No spaces are allowed.

A class name like Example1, Add, Sum etc.

In Java every pre-defined class name start with capital letter. Such as JFrame, JLabel etc.

datatype1, datatype2,...,datatypen are the predefined data types in Java such as int, float, char etc.

instance-variable1, instance-variable2,...,instance-variablen are the variablenames.

return-type: Every method of Java Program must have a return type. We have no need to declare a constructor method without any return type. The Java Programming Language consider the default return type for a constructor as void.

parameter-list: parameter-list is the values which we are passing to that method.

See the following Hello World Program:

                       class HelloWorld {
                                public static void main(String args[]) {
                               System.out.println("Hello World...!!!");
                            }
                        }

Here public static void main(String args[]) is the default syntax of main() method in Java and String args[] is the command-line arguments.


To execute above program, It is easy to save the any Java Program with it's main class name. For example, save the above program as HelloWorld.java
After saving your program, goto command prompt type javac HelloWorld.java to compile the Java Program.
If your program has no errors then type java HelloWorld
Then output for your program will display in next line.

See this video -- https://www.youtube.com/watch?v=iVTIHlS8rVM

Both in the structure and HelloWorld example I haven't shown How to access methods of one class with object. I will show that in further classes.

03:44 Share:
About Kalyan Kurasala

I am a B.Tech Student at Prasiddha College Of Engineering and Technology in Anathavaram.

0 comments:

Get updates in your email box
Complete the form below, and we'll send you the best coupons.

Deliver via FeedBurner
back to top