Professional Documents
Culture Documents
<class-name>.Method-Name(<parameters>);
<object-reference>.Method-Name(<parameters>) or
<object-reference>.Method-Name(<parameters>)
<class-name>.Method-Name(<parameters>);
<object-reference>.Method-Name(<parameters>) or
<object-reference>.Method-Name(<parameters>)
3. Object Methods can access both static 3. Class Methods can only access static
as well as non-static fields fields. Object Fields in a static method
are accessible only by creating objects
of the class
d = 89.67;
e = 67.89;
}// End of Method
public void display()
{
System.out.println("a= "+this.a+" b= "+this.b+" c= "+this.c +" d= "+d+" e= "+e);
} // End of Method
11 }// End of class Demo Programming (CS F213)
Object-Oriented Dr. Pankaj Vyas
Accessing Object and Class
Methods : Example 1
// File Name : Demo.java
class Demo
{
double a;
double b;
double c; F:\>javac Demo.java
static double d;
static double e; Demo.java:22: non-static variable this cannot be
public void doS()
{ referenced from a static context
this.a = 30; this.a = 30;
this.b = 40;
Object c = 50; ^
Method d = 89.67;
Demo.java:23: non-static variable this cannot be
e = 67.89; referenced from a static context
display();
this.b = 40;
}// End of Method ^
public static void doS1()
{ Demo.java:24: non-static variable c cannot be
this.a = 30; referenced from a static context
class this.b = 40;
Method c = 50; c = 50;
d = 89.67;
^
e = 67.89; 3 errors
}// End of Method
public void display()
{
System.out.println("a= "+this.a+" b= "+this.b+" c= "+this.c +" d= "+d+" e= "+e);
} // End of Method
12 }// End of class Demo Programming (CS F213)
Object-Oriented Dr. Pankaj Vyas
Accessing Object and Class
Methods : Example 2
// File Name : Demo.java // Object Method-2 display
class Demo public void display()
{ {
double a; System.out.println("a= "+this.a);
double b; System.out.println("b= "+this.b);
double c; System.out.println("c= "+this.c);
static double d; System.out.pritnln("d= "+d);
static double e; System.out.println("e= "+e);
// Object Method-1 doS }// End of Method
public void doS()
{ }// End of class Demo
this.a = 30;
this.b = 40; // Driver Class
c = 50; class Test
d = 89.67; {
e = 67.89; public static void main(String args[])
display(); {
}// End of Method Demo a1 = new Demo();
// Class Method doS1
public static void doS1() a1.doS();
{
d = 189.67; Demo.doS1();
e = 167.89;
}// End of Method a1.display();
F:\>java Test
a= 30.0
b= 40.0
c= 50.0
d= 89.67
e= 67.89
a= 30.0
b= 40.0
c= 50.0
d= 189.67
e= 167.89