Results 1 to 9 of 9
Thread: about static thing
- 07-07-2011, 06:56 PM #1
Member
- Join Date
- Jan 2011
- Posts
- 12
- Rep Power
- 0
about static thing
I just wonder why about the 4th constraint for field/method with static modifier. Why can't class methods access instance variables or instance method directly?
I noticed it from Understanding Instance and Class Members (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
Not all combinations of instance and class variables and methods are allowed:
1.Instance methods can access instance variables and instance methods directly.
2.Instance methods can access class variables and class methods directly.
3.Class methods can access class variables and class methods directly.
4.Class methods cannot access instance variables or instance methods directly—they must use an object reference. Also, class methods cannot use the this keyword as there is no instance for this to refer to.
- 07-07-2011, 07:52 PM #2
Because instance (non-static) variables and methods belong to a particular instance of a class. Class (static) variables and methods do not belong to a particular instance of a class. If a static method tried to access a non-static variable, which instance's variable should it be accessing? Keep in mind, that 10 different instances can have 10 different values for any of the non-static variables.
If you don't understand what I'm talking about, throw together an example SSCCE and we'll go from there.How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 07-07-2011, 07:57 PM #3
Member
- Join Date
- Jul 2011
- Posts
- 7
- Rep Power
- 0
When you create an object, you create an instance of this object. This instance is the only one that can access the non-static public methods of this object. If you access a non-static method from an object that doesn't exist, the program will crash since non of its variables and non-static methods had been created.
Imagine an object Person. When you create a Person you may set its name and last name. This object is the only one that can access its public instance variables and its public instance methods (set name, get name, set last name, get last name). Nobody else but this Person knows about its name or last name. If someone else needs this information, it is needed to be retrieved through the object Person like person.name();
When you declare a method public and static it means that you don't need to create an object before using it. Like Math.pow(x, y) it will be used for general purposes and can be called without creating an object. Although in this static methods you can't use anything that is non-static since you didn't create any object.
Does this make sense?
- 07-07-2011, 08:16 PM #4
I'm not trying to be pedantic, but some of what you said might be misleading:
That's not true. Anything that has the instance in scope can access the public methods of that Object.
I'm actually pretty sure this would be a compile-time error, not a program crash.
Again, if the instance variables (or methods) are public, then they can be accessed by any caller that has the instance in scope.
Or, you created many Objects and have no way of knowing which one to use. But yeah, if you're in a static context, it doesn't make sense to access non-static variables or methods.How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 07-08-2011, 05:24 PM #5
Member
- Join Date
- Jan 2011
- Posts
- 12
- Rep Power
- 0
how about this code?Java Code:public class X{ public static void accessY(){ System.out.println(Y.y); } } public class Y{ public int y; public y(){ this.y = 10; } }
- 07-08-2011, 05:41 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,380
- Blog Entries
- 7
- Rep Power
- 17
- 07-08-2011, 06:36 PM #7
Member
- Join Date
- Jan 2011
- Posts
- 12
- Rep Power
- 0
compilation error..~ hahaha....
if i can't access instance variable from static method, what if i have to access it badly, what should i do?
- 07-08-2011, 06:37 PM #8
Member
- Join Date
- Jan 2011
- Posts
- 12
- Rep Power
- 0
can i pass the object Y as an argument into the method?
public static void accessY( Y yyy )
{
System.out.println(yyy.y);
}
- 07-08-2011, 07:44 PM #9
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,380
- Blog Entries
- 7
- Rep Power
- 17
When people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
Error in Code: Non-static method cannot be referenced from a static context
By oaklandsbest in forum New To JavaReplies: 9Last Post: 06-10-2011, 12:40 AM -
Can't make static reference to non-static method -> huh?! Simple car prgm
By enerj in forum New To JavaReplies: 7Last Post: 09-24-2010, 05:09 AM -
non-static method getType cannot be referenced from a static contex
By Dekkon0 in forum New To JavaReplies: 4Last Post: 05-12-2010, 11:05 AM -
Error: Non-static method append(char) cannot be referenced from a static context
By paul in forum Advanced JavaReplies: 1Last Post: 08-07-2007, 05:05 AM -
Error: non-static variable height cannot be referenced from a static context at line
By fernando in forum AWT / SwingReplies: 1Last Post: 08-01-2007, 09:25 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks