Results 1 to 9 of 9
  1. #1
    Karenthian is offline Member
    Join Date
    Mar 2011
    Posts
    20
    Rep Power
    0

    Default [SOLVED] Interface return type invalid

    Hello there everyone, I'm in a bit of a predicament.
    I'm trying to create a class with an interface. My code is as follows, though I've taken out all my variables and such. The class only has a few lines in it because I didn't want to add much more when it won't work as is.
    Java Code:
    import java.util.*;
    import java.awt.*;
    import java.io.*;
    import javax.swing.*;
    
    public class main extends JPanel
    {
    	//Variables
    
    	private JTextField objectField;
    
    	static Scanner sc = new Scanner(System.in);
    	public static void main (String[] args)
    	{
    		new Interface();
    	}
    
    	public Interface ()
    	{
    		this.setTitle("Interface");
    		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		this.setLocationRelativeTo(null);
    		JPanel stuff = new JPanel ();
    			stuff.add(new JLabel("Object: "));
    			stuff.add(objectField = new JTextField(5));
    		this.add(stuff);
    		this.setVisible(true);
    	}
    }
    When I compile it I get an 'Invalid method deceleration; return type required' problem. I'm fairly sure that an interface doesn't actually return things but I can't figure it out.
    Sorry if this is a stupid mistake, thanks for any help!
    ~Karenthian~
    Last edited by Karenthian; 05-09-2011 at 11:59 PM.

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    The problem is that Interface is a constructor for a class called main. The constructor should match the name of the class. Either change the class name to Interface, or change the constructor to main.

    The compiler sees a method without a return type, since that's sort of what a constructor is. It doesn't see it as a constructor because it doesn't match the classes name.

  3. #3
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    Please read up on interfaces.

  4. #4
    Karenthian is offline Member
    Join Date
    Mar 2011
    Posts
    20
    Rep Power
    0

    Default

    Well that certainly fixed that, thank you. It's no longer throwing the return type error, but now it's throwing a 'cannot find symbol' error with the setTitle, setDefaultCloseOperation and setLocationRelativeTo lines. Is it my clumsy code again?
    Sorry to be a bother with this.
    ~Karenthian~

  5. #5
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    Quote Originally Posted by Karenthian View Post
    Well that certainly fixed that, thank you. It's no longer throwing the return type error, but now it's throwing a 'cannot find symbol' error with the setTitle, setDefaultCloseOperation and setLocationRelativeTo lines. Is it my clumsy code again?
    Sorry to be a bother with this.
    ~Karenthian~
    You're trying to use JFrame methods for a JPanel. Extend JFrame; not JPanel. Also, you don't need the "this." before the methods.

  6. #6
    Karenthian is offline Member
    Join Date
    Mar 2011
    Posts
    20
    Rep Power
    0

    Default

    D'Oh! That would explain it, and you say I don't need the 'this.', do you mean I can just put any method without the void command? The book I've been using (the dummies all in one desk reference) has stated a few times that you should declare methods like this as 'this.whatEver'. Is there any particular advantage to using the 'this.' method then? If not it seems to be superfluous code.
    Thanks for the help though, it's running perfectly now and I can hopefully finish it off! Sorry for the rookie questions here!
    ~Karenthian~

  7. #7
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    392
    Rep Power
    4

    Default

    this.<method> is, as far as I can think of, never needed. this refers to the current object and is generally only used in order to refer to it as an argument in method calls.

  8. #8
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    The this keyword let's you reference the class you are in. It is not however necessary to call the method directly in the class. You can if you really want and it may convey what the method is working on but it is entirely unnecessary.

  9. #9
    Karenthian is offline Member
    Join Date
    Mar 2011
    Posts
    20
    Rep Power
    0

    Default

    Brilliant! We'll you've taught me a lot guys, thanks for being so helpful :D
    ~Karenthian~

Similar Threads

  1. JavaBean - invalid method declaration and return type required
    By jprog in forum Enterprise JavaBeans (EJB)
    Replies: 1
    Last Post: 05-07-2011, 06:13 PM
  2. Invalid Method Declaration; Return Type Required
    By bremzb in forum AWT / Swing
    Replies: 3
    Last Post: 05-05-2011, 10:12 PM
  3. Err: invalid method declaration; return type required
    By Die The Villain in forum New To Java
    Replies: 12
    Last Post: 04-15-2011, 12:44 AM
  4. invalid method declaration; return type required
    By XaviannNJ in forum JCreator
    Replies: 7
    Last Post: 11-25-2010, 01:59 PM
  5. Replies: 3
    Last Post: 06-05-2010, 08:05 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
  •