Code structure in java

Elements present in a java code file

Source file The source file holds one class definition. The class represents a piece of your program, although the very tiny application must need a single class the class must go inside the curly braces. Class file A class has one or more methods. A method holds the instructions for what the program has to do. If the program is to add two numbers then the method holds the instructions for adding those two numbers. These methods must be declared inside a class with in the curly braces. Method Within the curly braces of a method write the instructions on how a method has to perform. Method code is basically a set of statements and for now it can be considered as a method or a procedure. Put a class in a source file, method inside a class and statements inside a method.

Dissection of a class
When the JVM starts running it looks for the class that you give at the command line. Then it starts looking for the method which look exactly like. public static void main(String args[]) { } Next JVM runs everything inside this main method. Every java program has to have at least one class and at least one method ( not one main per class but one main per application).

Elements present in the code and thier defination

A java program can be defined as a collection of objects which communicate invoking each other’s methods. Now let’s have a look on what class, object, instance variables and methods mean. Object – They have states and behaviors. Example: : A dog has states - color, name, breed as well as behavior such as wagging their tail, barking, eating. An object is an instance of a class. Class – This can be defined as a blue print that describes the behavior that object of its type supports. Methods – This is basically a behavior. A class contains many methods. It is in methods where the logics are written, data is manipulated and all the actions are executed. Instance Variables – Every object has its unique set of instance variables. The state of a object is created by the values assigned to these instance variables.

Keywords in Java

The following list shows the keywords that are used in java. These cannot be used as constant, variable or any other identifier names.  Insert image here

Identifiers in java

All Java components require names. Names used for classes, variables, and methods are called identifiers. In Java, there are several points to remember about identifiers. They are as follows −

  • All identifiers should begin with a letter (A to Z or a to z), currency character ($) or an underscore (_).
  • After the first character, identifiers can have any combination of characters.
  • A key word cannot be used as an identifier.
  • Most importantly, identifiers are case sensitive.
  • Examples of legal identifiers: age, $salary, _value, __1_value.
  • Examples of illegal identifiers: 123abc, -salary.

Java Modifiers

There are two categories of modifiers

Access Modifiers

default When no access modifier is specified for a class , method or data member. It is said to be having the default access modifier by default. The data members, class or methods which are not declared using any access modifiers i.e. having default access modifier are accessible only within the same package.
In this example, we will create two packages and the classes in the packages will be having the default access modifiers and we will try to access a class from one package from a class of second package. Private The private access modifier is specified using the keyword private.

  • The methods or data members declared as private are accessible only within the class in which they are declared.
  • Any other class of same package will not be able to access these members.
  • Top level Classes or interface can not be declared as private because

private means “only visible within the enclosing class”. protected means “only visible within the enclosing class and any subclasses” Hence these modifiers in terms of application to classes, they apply only to nested classes and not on top level classes Protected
The protected access modifier is specified using the keyword protected.
The methods or data members declared as protected are accessible within same package or sub classes in different package. In this example, we will create two packages p1 and p2. Class A in p1 is made public, to access it in p2. The method display in class A is protected and class B is inherited from class A and this protected method is then accessed by creating an object of class B. Public The public access modifier is specified using the keyword public.

  • The public access modifier has the widest scope among all other access modifiers.
  • Classes, methods or data members which are declared as public are accessible from every where in the program.
  • There is no restriction on the scope of a public data members.

Non-access Modifiers


  • Final class – When a class is set to final, it cannot be extended by any other class. It is a final segment of the hierarchy of the class
  • Final Method – When method is set to final it cannot be over ridden by any child class. The final modifier prevents a method from being modified or over ridden by any child class
  • Final variables – When we set a variable to final the value of that cannot be changed. Final variables are like constants. Reference variable declared final cannot be re-assigned to another object again. How ever the data with-in the object can be changed. Final variable can be explicitly initialized only once.


Abstract class

When a class is declared as abstract it can never be instansiated again. The sole purpose for using abstract is for class to be extended.

  • A class cannot be declared both abstract and final
  • If a class contains one or more abstract methods then the class should be declared abstract, or else a compile error will be thrown.
  • An abstract class may contain both abstract methods as well normal methods.
  • An abstract class does not need to contain abstract methods.

Abstract Methods

The methods can be defined as such methods which doesn’t have body but has only signature. The method is provided by sub class. These methods can never be final. Any class that extends an abstract class must implement all the abstract methods of the super class unless the subclass is also an abstract class.

strict fp

When we use strict fp keyword it restricts floating point calculations to ensure probability. This command was introduced into Java with the Java virtual machine (JVM) version 1.2 and is available for use on all currently updated Java VMs.

This also ensures that you get exactly the same results from the floating point calculations on every platform. If you don’t use strict fp, the JVM is set free to use extra precision where available.

This can be used on classes, interfaces and non-abstract methods. When applied to a method, it causes all calculations inside the method to use strict floating-point math. When applied to a class, all calculations inside the class use strict floating-point math.

About Author

This is Krishna.I am the author of this blog. I am a technology enthusiast. If you found this article helpful please share it with your friends. Please provide your valuable comments on this blog.

Share this Article Facebook
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions