Results 1 to 5 of 5
- 05-12-2010, 05:05 AM #1
Member
- Join Date
- May 2010
- Posts
- 4
- Rep Power
- 0
non-static method getType cannot be referenced from a static contex
Java | public class Subtyping { - Subtyping Java Error - aiwgVK1r - Pastebin.com
Learning Java, the code in my eyes seems correct but if someone could help it would be much appreciated.
- 05-12-2010, 05:30 AM #2
Member
- Join Date
- May 2010
- Posts
- 14
- Rep Power
- 0
well, i dont know what your objective is, but i rewrote your code so it would compile and run.
now, im no java expert by any means. this code compiles and runs, though. hopefully someone who knows what they're talking about can follow me up and explain _why_ these changes worked / are necessary.Java Code:public class Subtyping { //you've got to declare these variables as static, at least static Object someObject = new Object(); static Integer someInteger = new Integer(10); public static void getType(Object object) { System.out.println(object.getClass().getName()); } public static void main(String[] args) { //you have to perform this operation inside of the main method someObject = someInteger; getType(someObject); } }
- 05-12-2010, 06:16 AM #3
Member
- Join Date
- May 2010
- Posts
- 4
- Rep Power
- 0
- 05-12-2010, 06:18 AM #4
Member
- Join Date
- May 2010
- Posts
- 14
- Rep Power
- 0
im at the tail end of my first java class right now, though ive been programming for a while. i remember being weirded out by the whole static/scope thing earlier in the semester.
once you get into projects that actually use separate classes in object oriented style, it all sorta falls into place and makes more sense.
.02 :)
- 05-12-2010, 11:05 AM #5
what happens behind the scenes is that static method main has no this reference to its class, so you can't call other methods in your class, because the compile replace your method call getType(someObject) with this.getType(someObject), which cause an error like "Cannot make a static reference to the non-static method ...". you can fix the problem declaring your methods inside the "main"-class also as static, like the examples above or instantiating your class and then call the method with yourInstance.getType(someObject).
Similar Threads
-
non static method cannot be referenced......
By Clown in forum New To JavaReplies: 2Last Post: 03-21-2010, 07:12 PM -
non-static method cannot be referenced from a static context.
By blackstormattack in forum New To JavaReplies: 5Last Post: 05-07-2009, 04:05 AM -
non-static method add(double,double) cannot be referenced from a static context
By cravi85 in forum Java SoftwareReplies: 5Last Post: 03-21-2009, 09:32 PM -
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