Total members 11893 |It is currently Tue Nov 05, 2024 12:01 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





Java Program composed of classes and a class consists of methods and variables. I will give you an introduction to elements of Java program here.

1. Classes and Objects:

Objects are the core of Java Programs , Object may represent a real world problem which we are trying to found solution to .Object is an instance of a class .In Java Program you will write classes and when the program run , objects will be created and do the task .

The Java Class has two components:
  1. Class declaration.
  2. Class Body.

The Class declaration contains the class name and the access modifier .The access modifier defines the accessibility of the class. There are other properties you can set in the declaration like inheritance from other class (Using extend keyword), or the class can't be base for other classes (Using final keyword) or no objects can't be created from this class (Using abstract keyword).

The Class body comes after the class declaration and it is contained within braces. The class body contains functions and variables.

The process of creating an object from the class called "instantiation" .That is why objects are also called instances of classes. Objects can be books, cars, buildings etc .For a example all books are represented by class called book .Each object has status , for example book have a name , category and price , these characteristics are called properties. Each object has a behavior, for example "OpenBook", "CloseBook", these behaviors is called methods.

2. Methods:

The methods contain the logic of the application, it do operation on the data. Each method has a declaration and body .The declaration includes the method name, return type, parameter, and accessibility. The body contains what method do, which determine the behavior of the object.

The method can be static this means, it can be invoke without referencing to an object. For example the main function of your application.

3. Variables:

Variables are the container of data. Object state is carried by variables. Variables have declaration which specifies variable name, type and accessibility. The variable must have legal identifier (name) and not a java keyword.


Rules of java legal identifier:

  1. Start with [a-z][A-Z] , or _ or $ .Don't begin with digit.
  2. First character can by followed by any series of letters or digits.
  3. Must not be a keyword.

The declaration of variable :

Should look like this:
Code:
<Type> <identifier>;


You can assign a value to the identifier:
Code:
<Type> <identifier> =<value>;


If your data type is a class, your declaration should be like this:
Code:
<classname> <identifier>=new <classname>() ;




_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time

Have been trying to do this.
But I had been having small problems with it.
Help please.


Author:
Newbie
User avatar Posts: 3
Have thanks: 0 time

rein08 wrote:
Have been trying to do this.
But I had been having small problems with it.
Help please.


Post your code... We help ya!

And great article., Don't forget to ignore <> marks!

_________________
Coding my life with Java, PHP, JavaScript, and Python


Author:
Newbie
User avatar Posts: 39
Have thanks: 15 time
Post new topic Reply to topic  [ 3 posts ] 

  Related Posts  to : Elements of a Java Program
 java program     -  
 java program     -  
 java chat program.     -  
 compiling and getting of a java program     -  
 email through JAVA program     -  
 Executing a Java Program     -  
 Need Java program for display the CPU usage     -  
 Java Chat Program between two computers     -  
 Java Program for communicate PC and Mobile     -  
 Java Chat Program with client & Server     -  



Topic Tags

Java Basics






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com