Code structure in java
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.
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.
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
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 −
There are two categories of 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.
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
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.
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.
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.
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.
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.