Evan Darwin
Evan Darwin

Reputation: 860

Java Classes are extremely confusing to me

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();

Upvotes: 4

Views: 2768

Answers (9)

Michael Borgwardt
Michael Borgwardt

Reputation: 346270

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:

ReturnType r = methodName(param1, param2)

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:

MyClass m = new MyClass(param1, param2);

There are several problems in your code:

  • Helper has the correct name for a constructor, but because it declares a void return type, the compiler will treat it as a method.
  • Inputter doesn't even have the correct name for a constructor. To use it as a constructor with new, it would have to be part of a class called Inputter

Perhaps you should start out reading the introduction to OO part of the Java tutorial.

Upvotes: 10

Edwin Buck
Edwin Buck

Reputation: 70909

First, start with the basics.

Classes best represent nouns. That means a Class is a model of (typically) a thing. Methods best represent verbs on those nouns. Drifting away from this ideal is sometimes necessary; however, the further you stay away from such an ideal, the harder it will be to understand what is going on. With a nod to the exceptions, since you're a beginner let us not get wrapped up in the exception but follow the rule.

public class Person {

   private String name;

   private int age;

   public Person(String name) {
     this.name = name;
     this.age = -1;
   }

   public void setAge(int value) {
     if (value < 0) {
       throw new IllegalArgumentException("Age must be greater than zero");
     }
     this.age = value;
   }

   public int getAge() throws IllegalStateException {
     if (age < 0) {
       throw new IllegalStateException("Age was not set");
     }
     return this.age;
   }
}

Read through the class above, and use its style for your beginning programs. When you find that its style is hindering you more than helping you, then you might have found a place where other techniques are needed.

Upvotes: 0

Zirak
Zirak

Reputation: 39808

The new keyword is reserved for instantiating (fancy word for saying "making new") classes. The way your class is made, when you make a new Helper, a function is run. That is the construct function, and is named like the class.

Once you instantiate a class, you gain access to the goodies within it (exception is a static method/attribute, where anyone can access it); all within the class that isn't private or protected.

Now, a short intro on OOP (Object Oriented Programming): You have classes, which are basically blocks of functionality. Within these classes are two things: Methods and Attributes (many names for that, but that's what I call them.)

A Method is basically a good ol` function: It has an input and an output. An attribute is really like any other variable.

Now, in Java and many other OO languages, there's a separation between the class declaration and class instances. A class declaration is basically the static coded class; exactly what you put in the code. A class instance is taking the class declaration and putting it into use: You can change and use the methods and attributes inside them.

So, if you want to call Inputter, you should do it like this:

Helper bob = new Helper('Bloop');
bob.Inputter(42);

What happened here? We made a new variable called bob which has a type of Helper. When constructing the new Helper, we also run the constructor. Looking at the constructor function, we pass a value to it (in this case, 'Bloop'), and the function is run normally, without us having to manually call it.

Now we want to use the Helper class' Inputter method. For that, we need to access an instance of the Helper class (in our case bob), by using bob. (notice the dot), and then calling it like any other function: Inputter(parameters). Gluing it together we get bob.Inputter(parameters)

This was a really rather lame explanation of Object orientation that didn't cover that much, but it should get you started. I recommend getting a book about Java or reading online tutorials.

Upvotes: 0

You must create an instance of Helper Before you can use Inputter:

Helper x = new Helper("some string");

to use Inputter, try this:

//create a new helper
Helper x = new Helper("some string"); 
//use the Inputter method of the helper.
x.Inputter(1); 

The thing to understand here is that Helper is the class, x is an instance of a class, and Inputter is a instance method (which is different from a static method) in the Helper class.

Upvotes: 1

brandon
brandon

Reputation: 1230

The only object here is Helper. If you want to make a new helper, then you will instantiate it by saying

Helper X = new Helper("blah blah");

If you want to call Inputter then you just say

X.Inputter(1234);

Which will call the Inputter function for the specific instance X of Helper

Upvotes: 1

Bala R
Bala R

Reputation: 108947

You can call it like this

String someString = "some string";
Helper x = new Helper(someString);
int someInt = 1;
x.Inputter(someInt);

Upvotes: 0

Laurent Pireyn
Laurent Pireyn

Reputation: 6875

Inputter is not a class. It's a method of the Helper class. So you cannot instantiate it.

Upvotes: 0

Lukasz
Lukasz

Reputation: 7662

Inputter in your code is not a class. It is a method.

To make following statement correct:

Helper x = new Inputter();

you would need to create Inputter class that extends Helper class.

Upvotes: 0

bragboy
bragboy

Reputation: 35542

Inputter() that you have defined is a method or you can call it a behavior. You cannot create an instance for a behavior.

One more problem is that you cannot have return types on a constructor. Helper is the class name and the constructor is having a return type which is incorrect

Regarding your quesiton, if you want to call Inputter, you should do it something like the following.

Helper helper = new Helper("test");
helper.Inputter(100);

It is a good practice to start methods with smaller case letters.

Upvotes: 1

Related Questions