Structure of Java Program

11:33 PM 0 Comments A+ a-

Source File Layout/ Java Program Structure
A Java Technology source file takes the following form:
[<package_declaration>]
<import_declaration>*
<class_declaration>+

NOTE: - The plus (+) indicates one or more. To be meaningful, a source file must contain at least one class definition.
1.     The order of the items is important. That is, any import statements must precede all class declarations and, if you use a package declaration, it must precede both the classes and imports.
2.     The name of the source file must be the same as the name of the public class declaration in that file. A source file can include more than one class declaration, but only one class can be declared public.
3.     If a source file contains no public class declarations, then the name of the source file is not restricted. However, it is good practice to have one source file for every class declaration and the name of the file is identical to the name of the class.

Declaring Classes in Java
The Java Technology class declaration takes the following form:
<modifier>* class <class_name> {


<attribute_declaration>*
<constructor_declaration>*
<method_declaration>*
}
Example:
public class Vehicle
{
            private int x,y;                                 //declaring methods
            public void setValues(int a, int b)          //declaring methods
            {          
                        x = a;
                        y = b;
            }
}

Declaring Attributes
The declaration of an object attribute takes the following form:
<modifier>* <type> <name> [= <initial_value>];
Example:
public class Foo
{
            private int x;
            private float y = 1000.0F;
            private String name = “Harish Sharma”;
}

Declaring Methods
The method declaration in Java is similar to that used in C and C++.
The declaration takes the following basic form:
<modifier>* <return_type> <name> (<arguement>*)
{
            <statement>*;
}

Example:
public class Dog
{
            private int weight;
            public int getWeight()
            {
                        return weight;
            }
            public void setWeight(int newWeight)
            {
                        If(newWeight>0)
                        {
                                    Weight = newWeight;
                        }
            }

Accessing Object Members

Example
public class AccessObjects
{
public static void main(String arg[])
{
Dog d = new Dog();
System.out.println("Dog d's weight is"+ d.getWeight());
d.setWeight(45);
System.out.println("Dog d's weight is"+ d.getWeight());
d.setWeight(-45);
System.out.println("Dog d's weight is"+ d.getWeight());
}
}

Currently working at Symantec in Chennai. Previously worked as a Senior Software Engineer at IGATE. Loves to explore new things, loves to code and solve challenging problems with the help of it. Like any other normal person, loves to watch movies, listen to songs (specially when travelling, it is must), getting to know people, travelling and whatever makes me feel good!