Results 1 to 14 of 14
Thread: Interface help
- 04-07-2011, 04:59 AM #1
Member
- Join Date
- Apr 2011
- Posts
- 9
- Rep Power
- 0
Interface help
Hi there... I'm teaching myself Java, and I'm trying to understand interfaces.
Here's a bit of code I'm playing with, and I'm wondering what I'm doing wrong
Can anyone point me in the right direction?Java Code:public class StudentDemo { public void main(String[] args) { Student stu = new Student(); stu.setName("wallace"); stu.setYearOfCommencement(1101); } public class Student implements StudentSpecification { String stuName = null; int stuYear = 0; public void setName(String name); { stuName = name; } public void setYearOfCommencement(int year); { stuYear = year; } public String getName() { return stuName; } public int getYearOfCommencement(); { return stuYear; } } interface StudentSpecification { // Change the name of the student void setName(String name); // Change the year of commencement void setYearOfCommencement(int year); // Get the name String getName(); int getYearOfCommencement(); } }
I'm finding alot of information online, but the examples the use never pass arguments like mine does.
- 04-07-2011, 05:02 AM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
What problems are you running into? Try creating classes and interfaces as separate things, not inside the tester class.
- 04-07-2011, 05:04 AM #3
How about prividing relevant information about what is wrong. Do you get errors? then post the EXACT error messages and indicate on which line they occur. Doe the code run but not produce correct output? Then show the output you get and what you expect instead. Don't make us guess.
- 04-07-2011, 05:06 AM #4
Just spotted some problems. In the Student class compare the getName method to all your other methods. Notice anything amiss?
- 04-07-2011, 05:06 AM #5
Member
- Join Date
- Apr 2011
- Posts
- 9
- Rep Power
- 0
Terribly sorry... When I compile, I see this:
And I dont see what difference having the interface class does in this case?Java Code:StudentDemo.java:14: missing method body, or declare abstract public void setName(String name); ^ StudentDemo.java:16: cannot find symbol symbol : variable name location: class StudentDemo.Student stuName = name; ^ StudentDemo.java:19: missing method body, or declare abstract public void setYearOfCommencement(int year); ^ StudentDemo.java:21: cannot find symbol symbol : variable year location: class StudentDemo.Student stuYear = year; ^ StudentDemo.java:29: missing method body, or declare abstract public int getYearOfCommencement(); ^ StudentDemo.java:31: return outside method return stuYear; ^ 6 errors
- 04-07-2011, 05:07 AM #6
See my reply above
- 04-07-2011, 05:08 AM #7
Member
- Join Date
- Apr 2011
- Posts
- 9
- Rep Power
- 0
Oh god... That was a classic monday moment!
- 04-07-2011, 05:10 AM #8
Member
- Join Date
- Apr 2011
- Posts
- 9
- Rep Power
- 0
Ok... I need to have my main method static, but in doing so I run into:
At line 5Java Code:non-static variable this cannot be referenced from a static context
- 04-07-2011, 05:12 AM #9
Post your latest code.
- 04-07-2011, 05:21 AM #10
Member
- Join Date
- Apr 2011
- Posts
- 9
- Rep Power
- 0
Returns this:Java Code:public class StudentDemo { public static void main(String[] args) { Student stu = new Student(); stu.setName("wallace"); stu.setYearOfCommencement(1101); } public class Student implements StudentSpecification { String stuName = null; int stuYear = 0; public void setName(String name) { stuName = name; } public void setYearOfCommencement(int year) { stuYear = year; } public String getName() { return stuName; } public int getYearOfCommencement() { return stuYear; } } interface StudentSpecification { // Change the name of the student void setName(String name); // Change the year of commencement void setYearOfCommencement(int year); // Get the name String getName(); int getYearOfCommencement(); } }
Java Code:javac *.java StudentDemo.java:5: non-static variable this cannot be referenced from a static context Student stu = new Student(); ^ 1 error
- 04-07-2011, 05:33 AM #11
It is a rather odd error but it is caused by having Student and StudentSpecification in the same file as StudentDemo. You can either make Student static (not recommended) or place Student and StudentSpecification in their own files.
- 04-07-2011, 05:49 AM #12
Member
- Join Date
- Apr 2011
- Posts
- 9
- Rep Power
- 0
Thanks so much for your help!
- 04-07-2011, 08:40 AM #13
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
It's actually caused by having Student inside StudentDemo, not by them simply being in the same file. Consequently Student can only be defined with respect to an instance of StudentDemo, unless Student was to be defined as static.
But the best idea is, as you say, to make them their own files.
- 04-07-2011, 11:42 PM #14
Similar Threads
-
Why INTERFACE...pls tell me please....
By rohitjava in forum New To JavaReplies: 3Last Post: 09-02-2010, 10:27 AM -
Interface?
By MarkWilson in forum New To JavaReplies: 4Last Post: 07-11-2008, 09:10 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks