• Post Reply Bookmark Topic Watch Topic
  • New Topic

Static method and instance variable  RSS feed

 
Faraz Alig
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Not very sure if I had understood the concept of static variable /methods but the below piece of code is causing some trouble.

public class Foo {
int i = 5;
static int k = 2;

public static void main(String[] args) {
int j = i; // Wrong because i is an instance variable
m1(); // Wrong because m1() is an instance method
}

public void m1() {
// Correct since instance and static variables and methods
// can be used in an instance method
i = i + k + m2(i, k);
}

public static int m2(int i, int j) { // -> A
return (int)(Math.pow(i, j)); // -> B
}
}

As instance variables and methods can only be used from instance methods, not from static methods, then why at // A and //B I am not getting the error
m2 is an static methods and its using i, which is an instance variable.

Please clarify this doubt.

Thanks,
Faraz
 
Nick Puketza
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Faraz,

The "i" in your m2 method is a parameter which just happens
to have the same name as the instance variable "i". Even though
they have the same name, the parameter and the instance
variable are separate things. For example, if you were to
add the assignment statement "i = 10;" to the m2 method,
the assignment would affect the parameter "i" but not the
instance variable "i".

I hope that helps,
- Nick
 
Geoff Jefferson
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all You have main() defined within Foo, and it should not be.
Also, It appears that you are defining Foo as a class that contains
i, and k. If so, then they will be visible only in Foo.
A static member is one that any instance of that class will have access to
and is usually defined in the body of the class, not in a method.
Static methods can be used to access static members for example.
check out the following link:

http://java.sun.com/docs/books/tutorial/java/javaOO/classvars.html


 
Geoff Jefferson
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, It appears that you are defining Foo as a class that contains

Pardon me, I meant to say method and not class in this line.

 
Nick Puketza
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Faraz,

First, thanks to Geoff for his comments about static variables.

I just wanted to add one more thing to my post. The following
program might make what I was saying more clear.



The output should be:
5
100
200
5

I just wanted to demonstrate that the "i" in m2() is
separate from the instance variable "i" in the
Foo object that is created in main().

- Nick
 
Faraz Alig
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everyone for the valuable comments. Now the concept is becoming more clear

Regards,
Faraz
 
Sha Jar
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Faraz Alig wrote:
Not very sure if I had understood the concept of static variable /methods but the below piece of code is causing some trouble.


Anything static is what you really would like not to be part of an object if Java would allow you to.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!