Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

What is this?  RSS feed

 
Frances Hollis
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is part of the code that was provided by my instructor. We are to add some constructor methods to it, which I understand. I don't, however, have a clue what this is.

private StuList(){};

Why the braces?

Thanks for any help. Frances
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's an empty default (no-argument) constructor for a class named "StuList". The braces are the empty body -- i.e., there are no statements in it. It doesn't do anything. It's also private, meaning that it can't be called outside of this class. This is a common Java trick to prevent instances of the class from being created; for example, the java.lang.System and java.lang.Math class have private constructors like this.
[ September 30, 2006: Message edited by: Ernest Friedman-Hill ]
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yep ! Classes like these usually have a getInstance() method that will return an instance of the object you want. This way you can control the type of object that the requestor recieves. You can also prevent class instantiation all together and use the class as a kind of helper class.
 
Frances Hollis
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, then why this compiler error?

C:\Documents and Settings\Ann\Desktop\StuListAnn.java:12: invalid method declaration; return type required
private StuList(){};
^
1 error

Tool completed with exit code 1
 
Robert Hill
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A constructor has to have the exact same name as its class.

Since it isn't exact the compiler thinks it is supposed to be a method, and methods must have a return type.
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check the spelling. Make sure the constructor name is pronounced with the same casing as the class name.

-Cameron
 
Frances Hollis
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, spelling was the problem. When the teacher gives us code to start with I always save it with a new name. I always have to remember to change everything else. :roll:
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Frances Hollis:
Thanks, spelling was the problem. When the teacher gives us code to start with I always save it with a new name. I always have to remember to change everything else. :roll:

Hmmm... You might want to rethink that practice, since it sounds like you're making trouble for yourself.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!