Static Members
The static members can be static data member or static method. The static members are those members which can be accessed without using object. Following program illustrates the use of static members.
• static members
1. static member
2. static method
Java Program [Static Prog.java]
/*************************************************************
Program to introduce the use of the static method and static variables
class StaticProg
{
static int a = 10;
static void fun(int b)
{
System.out.println("b= "+b);
System.out.println("a = "+a);
}
}
class AnotherClass
{
public static void main(String[] args)
{
System.out.println("a= "+Static Prog.a);
StaticProg.fun(20);
}
}
Output
a= 10
b= 20
a= 10
Points to remember about static members:
•In Java the main is always static method.
•The static methods must can access only static data.
• The static method can call only the static method and cannot call a non-static method.
• The static method cannot refer to this pointer.
• The static method cannot refer to super method.
JavaDoc Comments
Javadoc is a convenient, standard way to document your Java code. Javadoc is actually a special format of comments. There are some utilities that read the comments, and then generate HTML document based on the comments. HTML files give us the convenience of hyperlinks from one document to another.
There are two types of Javadoc comments -
• Class level comments
• Member level comments
The class level comments describe the purpose of classes and member level comments describe the purpose of members.
The Javadoc comments start with /** and end with */ For example
/** This is a Javadoc comment statement*/
The class level comments provide the description and purpose of the classes. For example - /**
*@author XYZ
* The Employee class contains salary of each employee the organization
*/
public class Employee
{
//Employee class code
}
The member level comments describe the data members, methods and constructors used in particular class. In this type of comments special tags are used to describe the things. The most commonly used tags are -
The example of member level comments for the Employee class is as shown below /**
* @author XYZ
* The Employee class contains salary of each employee the organisation
*/
public class Employee
{
/**
*Employee information for knowing the salary
*/
private int amtSalary;
/**
*The constructor defined to initialise the salary amount
*/
public Employee()
{
this.salary=0;
}
/**
* This method returns the salary amount of particular employee
*@return int
*/
public int getAmtSalary()
{
return salary;
}
/**
*@param int No_of_Days_Worked is for total number of working days
* @param int Payscale is for payment scale as per the designation of the employee
*/
public void setAmtSalary(int No_of_Days_Worked, int Payscale)
{
this.salary=No_of_Days_Worked* Payscale;
}
Along with the above mentioned tags some HTML tags can also be included in Javadoc comments. For example we can use<table><img> tags in Javadoc. Also we can include special tags like < in the Javadoc. Some words like true, false and null can also be included in Javadoc.
A) Static data members are unique to each instance of the class.
B) Static data members are shared by all instances of the class.
C) Static data members cannot be initialized.
D) Static data members can only be accessed inside the class.
A) By creating an object of the class.
B) By using the class name directly.
C) Static variables cannot be accessed outside the class.
D) Static variables can only be accessed through static methods.
A) static int count = 0;
B) int static count = 0;
C) static count = 0;
D) static var count = 0;
A) They help in memory optimization as they are shared by all objects.
B) They provide faster access to data.
C) They can only be used in non-static methods.
D) They are easier to maintain than instance variables.
A) A static variable can only be initialized inside a constructor.
B) A static variable is initialized when the class is loaded.
C) A static variable can only be accessed through a non-static method.
D) Static variables can be accessed only by static methods.
B) Static data members are shared by all instances of the class.
B) By using the class name directly.
A) static int count = 0;
A) They help in memory optimization as they are shared by all objects.
B) A static variable is initialized when the class is loaded.