Page 267 - CA_Blue( J )_Class10
P. 267
The instance variables are accessed by preceding it with the object name and dot operator. The syntax to access the
instance variables is:
<class object>.<instance variable name>
For example: ww
IVariables obj = new IVariables(); //creating object of class
Obj.a; //calling instance variable
Class Variables
A class variable is also known as a static data member. It is a variable that is declared within the class using a static
keyword. Only one copy is created for all the objects of the class thus having a single value of the variable for all the
objects. For example:
class CVariables
{
static int a;
static double b;
//member methods
}
Class variables are accessed by using the class name and the operator. The syntax to access the class variables is:
<class name>.<class variable name>
CVariables.a; //calling static variable
Local Variables
A variable that is declared within a member method is called a local variable. It can be accessed only within the method
in which it is declared. We cannot use the static keyword to declare a local variable. It is necessary to initialise a local
variable before using it.
Program 2 To calculate the factorial of a number.
1 import java.util.*;
2 class factorial
3 {
4 int n;
5 void input()
6 {
7 Scanner sc= new Scanner(System.in);
8 System.out.println("Enter a number: ");
9 n=sc.nextInt();
10 }
11 void display()
12 {
13 int f=1,i;
14 for(i=1;i<=n; i++)
265
Class as the Basis of all Computation 265

