Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

static reference to nonstatic var & methods

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am confused about when and how static references can be made to non static variables and methods.
Consider this bit of code:
class Box {
double width;
double height;
double depth;
double volume() {
return width * height * depth;
}
void setDim(double w, double h, double d) {
width = w;
height = h;
depth = d;
}
}
class BoxDemo {
public static void main(String args[]) {
Box myBox1 = new Box();
Box myBox2 = new Box();
double vol;
myBox1.setDim(10, 20, 15);
myBox2.setDim(4, 9, 16);
vol = myBox1.volume();
System.out.println("Volume of myBox1 = " + vol);
vol = myBox2.volume();
System.out.println("Volume of myBox2 = " + vol);
}
}
This code compiles and runs fine. However in the main method of the BoxDemo class a reference is made to the non static variables and method of the Box class. But the compiler does not protest.
Now consider this piece of code:
class MyPoint1 {
int x, y;
void myMethod() {
System.out.print( " ( " + x + ", " + y + " ) " );
switchCoords( x, y );
System.out.print( " ( " + x + ", " + y + " ) " );
}
void switchCoords( int x, int y ) {
int temp;
temp = x;
x = y;
y = temp;
System.out.print( " ( " + x + ", " + y + " ) " );
}
}
class MyPoint {
public static void main(String args[]) {
MyPoint1 MP1 = new MyPoint1();
MyPoint1.x = 5;
MyPoint1.y = 3;
MyPoint1.myMethod();
System.out.println();
}
}
Here the compiler protests the it can't make static reference to nonstatic variables and methods.

Can anybody please help me understand the difference between the two codes.
Thanks
 
Ranch Hand
Posts: 396
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi ajit,
everything is happening in correct way. look how :-)
class BoxDemo {
public static void main(String args[]) {

Box myBox1 = new Box();
Box myBox2 = new Box();

in above 2 lines u 've created to instances of Box namely myBox1 and myBox2.

now u called the non static method setDim() for two different instances which is perfectly legal.
myBox1.setDim(10, 20, 15); //setDim of myBox1 is getting // called
myBox2.setDim(4, 9, 16);//setDim of myBox1 is getting // called

on similar lines the following calls are equally legal.
vol = myBox1.volume();
vol = myBox2.volume();

so certainly This code compiles and runs fine.
However in the main method of the BoxDemo class a reference is made to the non static variables and method of the Box class. But the compiler does not protest.
here u r wrong coz we certainly accessed the the non static variables and method of the Box class but using instances i.e. using myBox1 and myBox2. so compiler knows that which non static method it has to call since it has got the object name associated with each call.
Now consider this piece of code:
class MyPoint {
public static void main(String args[]) {
MyPoint1 MP1 = new MyPoint1();
here also u created the instance MP1 of MyPoint class [B}BUTTTT[/B]

MyPoint1.x = 5; // u r using class name to
MyPoint1.y = 3; // access the
MyPoint1.myMethod(); // non static variables

so u tried to access non static variables & method using class name. which compiler can't accept. had u used
MP1 instead of MyPoint1 compiler would have been happy.
so remember
==========================================================
To Access use
==========================================================
1) static members class name
2) non static members object (instance) name.
==============================================================
do notify me if it was of any help
regards
deekasha
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I couldn't find any instance variable being referred using Class name in code 1.
In main method, instance of Box class (myBox1, myBox2) are created. These instance are used to invoke member methods.
// myBox1 and myBox2 are instances of class Box.
myBox1.setDim(10, 20, 15);
myBox2.setDim(4, 9, 16);
vol = myBox1.volume(); // vol is a local variable

In code 2, a member method is tried to access using Class name instead of an instance.
MyPoint1 MP1 = new MyPoint1();// Mp1 is the instance
MyPoint1.x = 5;// MyPoint1 is a Class and not an instance
MyPoint1.y = 3;// MyPoint1 is a Class and not an instance
MyPoint1.myMethod();// MyPoint1 is a Class and not an instance
change the code as follows to make it work :
MyPoint1 MP1 = new MyPoint1();
MP1.x = 5;
MP1.y = 3;
MP1.myMethod();

Hope, this helps.
cheers
Sankar S
 
rubbery bacon. crispy tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic