• Post Reply Bookmark Topic Watch Topic
  • New Topic

Having problems  RSS feed

 
maya tomarchio
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys!! still new to java and having a few problems. I am using two classes and am getting an error message on Lesson16.saying in the first class. The error message says "cannot make static reference to the non-static method saying() from the type Lesson16" I am not sure what this means or how to fix it. Any suggestions? thanks.





 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to call saying() method on object of the class you have created. An instance of the class (object) reference variable in your case is "Lesson16object".
I think the main confusion was caused by the poorly chosen variable names.
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are some other places you could improve:

1. Try to avoid such class names as Lesson16b. It is very difficult to link it with program's context, and after all it doesn't make sense to have an object lesson16b, where Person would be more appropriate.

2. Try to pay more attention to code formatting. Indentation is ok.
For formatting leave an empty line between method declarations. Put one leading and trailing space around operators as -, +, =, etc..

3. For method names also try to find some better names, i.e:
So, without knowing method's sayYourName() implementation you know what to expect from it. While in your case saying() isn't that brutally clear what it supposed to be saying.

4. According to Java convention, method names, same as variable names supposed to be named like getName() instead getname(), firstName instead firstname.
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome again

Have you been told to use that structure for a class? Have you not been taught how to write a constructor? When you create an instance of that class, the field will have its default value, because you have never initialised it. There is no guarantee that the setname method (which shou‍ld read setName) will ever be called. The constructor taking a name as its parameter would force you to pass a value which is assigned to the field.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!