Class in Java

In this section, we will discuss about java classes and
its structure.
First of all learn: what is a class in
java and then move on to its structural details.
Class: In the object oriented approach, a class
defines a set of properties (variables) and methods. Through methods
certain functionality is achieved. Methods act upon the variables and
generate different outputs corresponding to the change in variables.
Structure for constructing java program:
package package_name;
//import necessary packages
import package_name.*;
Access Modifier class class_name{
member variables;
member methods;
public static void main(String[] args) {
...
....
}
} |
package: In the java source file you
generally use the package
statement at header of a java program. We use package generally to
group the related classes i.e. classes of same category or related
functionality. You have to save your java files in the folder matching the
package name as mentioned in java program.
import : Import keyword is used at the beginning of a source file
but after the package statement. You may need some classes, intefaces
defined in a particular package to be used while programming so you would
have to use import statement. This specifies classes or entire Java packages
which can be referred to later without including their package names in the
reference. For example, if you have imported util package (import
java.util.*;) then you need not to mention the package name while using
any class of util package.
Access Modifiers : Access modifiers are
used to specify the visibility and accessibility of a class, member
variables and methods. Java provides some access modifiers like: public, private etc..
These can also be used with the member variables and methods to specify
their accessibility.
- public keyword specifies
that the public class, the public fields and the public methods can be accessed from anywhere.
- private: This keyword
provides the accessibility only within a class i.e. private fields and methods can
be accessed only within the same class.
- protected: This modifier makes a member
of the class available to all classes in the same package and all sub
classes of the class.
- default : Its not a keyword. When we
don't write any access modifier then default is considered. It allows
the class, fields and methods accessible within the package only.
static keyword indicates that this
method can be invoked simply by using the name of the class without
creating any class-object.
void keyword specifies that this method will not return any type of
data.
main() method is the main entry point of the program, to start
execution. First of all JVM calls the main method of a class and
start execution . JVM (Java Virtual Machine) is responsible for
running java programs.
args is a string array that takes values from java command line.
It's index starts from '0'. We can access values by writing args[0],
args[1] etc.
println() function prints the output to the standard output stream
(monitor).
out represents the standard output stream (monitor).

|
Current Comments
7 comments so far (post your own) View All Comments Latest 10 Comments:There will be times when you will want to define a class member that will be used independently of any object of that class. Normally a class member must be accessed only in conjunction with an object of its class. However, it is possible to create a member that can be used by itself, without reference to a specific instance. To create such a member, precede its declaration with the keyword static. When a member is declared static, it can be accessed before any objects of its class are created, and without reference to any object. You can declare both methods and variables to be static. The most common example of a static member is main( ). main( ) is declared as static
because it must be called before any objects exist.
Instance variables declared as static are, essentially, global variables. When objects of its class are declared, no copy of a static variable is made. Instead, all instances of the class share the same static variable.
Methods declared as static have several restrictions:
They can only call other static methods.
They must only access static data.
They cannot refer to this or super in any way. (The keyword super relates to
inheritance.)
If you need to do computation in order to initialize your static variables, you can declare a static block which gets executed exactly once, when the class is first loaded. The following example shows a class that has a static method, some static variables, and a static initialization block:
// Demonstrate static variables, methods, and blocks.
class UseStatic {
static int a = 3;
static int b;
static void meth(int x) {
System.out.println("x = " + x);
System.out.println("a = " + a);
System.out.println("b = " + b);
}
static {
System.out.println("Static block initialized.");
b = a * 4;
}
public static void main(String args[]) {
meth(42);
}
}
As soon as the UseStatic class is loaded, all of the static statements are run. First, a is set to 3, then the static block executes (printing a message), and finally, b is initialized to a * 4 or 12. Then main( ) is called, which calls meth( ), passing 42 to x. The three println( ) statements refer to the two static variables a and b, as well as to the local variable x.
Note It is illegal to refer to any instance variables inside of a static method. Here is the output of the program:
Static block initialized.
x = 42
a = 3
b = 12
Outside of the class in which they are defined, static methods and variables can be used independently of any object. To do so, you need only specify the name of their class followed by the dot operator. For example, if you wish to call a static method from outside its class, you can do so using the following general form:
classname.method( )
Here, classname is the name of the class in which the static method is declared. As you can see, this format is similar to that used to call non-static methods through object reference variables. A static variable can be accessed in the same way—by use of the dot operator on the name of the class. This is how Java implements a controlled version of global functions and global variables.
Here is an example. Inside main( ), the static method callme( ) and the static variable b are accessed outside of their class.
class StaticDemo {
static int a = 42;
static int b = 99;
static void callme() {
System.out.println("a = " + a);
}
}
class StaticByName {
public static void main(String args[]) {
StaticDemo.callme();
System.out.println("b = " + StaticDemo.b);
}
}
Here is the output of this program:
a = 42
b = 99
Posted by manoj kumar yadav on Tuesday, 01.29.08 @ 12:01pm | #46473
please differentiate the static and nonstatic variables with a program
Posted by srinunalluri on Saturday, 01.19.08 @ 09:20am | #45568
Hi I new to java.so i have no knowledge about java. so please help me to develope my java language .Thanku
Posted by babu on Friday, 01.18.08 @ 12:12pm | #45503
please differntiate instance and static variable with program
Posted by mahesh on Thursday, 08.23.07 @ 11:48am | #23955
u dont mention above example about import why use import key word ?
pls explain about import.
Posted by Aslam on Thursday, 08.16.07 @ 02:56am | #23479
u dont mention above example about import why use import key word ?
pls explain about import.
Posted by Aslam on Thursday, 08.16.07 @ 02:54am | #23478
u didnt mention any example for static keyword
plz explain static and state that how it is different from c++ static
Posted by sajida on Saturday, 06.9.07 @ 12:21pm | #18699