Results 1 to 5 of 5
  1. #1
    Dekkon0 is offline Member
    Join Date
    May 2010
    Posts
    4
    Rep Power
    0

    Default 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.

  2. #2
    musasabi is offline Member
    Join Date
    May 2010
    Posts
    14
    Rep Power
    0

    Default

    well, i dont know what your objective is, but i rewrote your code so it would compile and run.

    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); 
    	}
    }
    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.

  3. #3
    Dekkon0 is offline Member
    Join Date
    May 2010
    Posts
    4
    Rep Power
    0

    Default

    Quote Originally Posted by musasabi View Post
    well, i dont know what your objective is, but i rewrote your code so it would compile and run.

    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); 
    	}
    }
    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.
    Thanks, this actually makes alot of sense. Also, there wasn't an objective to this, it was just out of a example.

  4. #4
    musasabi is offline Member
    Join Date
    May 2010
    Posts
    14
    Rep Power
    0

    Default

    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 :)

  5. #5
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    Quote Originally Posted by Dekkon0 View Post
    Learning Java, the code in my eyes seems correct but if someone could help it would be much appreciated.

    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

  1. non static method cannot be referenced......
    By Clown in forum New To Java
    Replies: 2
    Last Post: 03-21-2010, 07:12 PM
  2. non-static method cannot be referenced from a static context.
    By blackstormattack in forum New To Java
    Replies: 5
    Last Post: 05-07-2009, 04:05 AM
  3. Replies: 5
    Last Post: 03-21-2009, 09:32 PM
  4. Replies: 1
    Last Post: 08-07-2007, 05:05 AM
  5. Replies: 1
    Last Post: 08-01-2007, 09:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •