I am having some trouble understanding classes in Java.
Such as how do you declare "Inputter" in the helper class like this?
public class Helper
{
public void Helper(String z)
{
if(z.length() == 0)
{
System.out.println("You can't leave it blank!");
System.exit(1);
System.out.println("It's not working... ;(");
}
}
public void Inputter(int a)
{
// blah blah
}
}
Would you call it like this?
Helper x = new Inputter();
Please help, and NO this is NOT a homework question.
Thanks,
Smiling
EDIT: Would this be right:
public class Helper
{
public Helper(String z)
{
if(z.length() == 0)
{
System.out.println("You can't leave it blank!");
System.exit(1);
System.out.println("It's not working... ;(");
}
}
public void Inputter(int a)
{
// blah blah
}
}
and declared with:
Helper x = Helper();
Your problem is not with classes, it is with constructors and methods, and the difference between them.
Methods can have any name you like, they must declare a return type (possibly
void), and they’re called like this:Constructors are used to create instances of classes (objects). They must have the same name as the class, they must not have a return type (not even
void), and they’re called like this:There are several problems in your code:
Helperhas the correct name for a constructor, but because it declares avoidreturn type, the compiler will treat it as a method.Inputterdoesn’t even have the correct name for a constructor. To use it as a constructor withnew, it would have to be part of a class calledInputterPerhaps you should start out reading the introduction to OO part of the Java tutorial.