Advertisement

Help Identifying lines of code

Started by June 12, 2017 09:39 AM
5 comments, last by Xer0botXer0 7 years, 5 months ago

Hi guys, so I'm going through a tutorial and they're provided the following code.


public class Puppy {
public Puppy() {
}

public Puppy(String name) {
// This constructor has one parameter, name.
}
}

I can see Puppy is a public class, it's not static or void, void means the class doesn't return a value. Can classes use void ? not sure about void and static yet.

So then there's public Puppy, is this a sub class, method or a constructor ? then there's public Puppy (string name) now I know this is a user created constructor.

So, in the case of the Puppy class above


public Puppy() {
} 

is a default constructor, so what it will use if you create the object without passing in a string for the name.

The next one is an overloaded constructor that takes a string, ideally in this case so you can give the puppy a name.

Advertisement

Okay thank you, I thought default constructors are created automatically, but this depends on the IDE I bet ? if I were to use the new keyword to create an object using notepad and run that in cmd, it would give me an error ?

Okay thank you, I thought default constructors are created automatically, but this depends on the IDE I bet ? if I were to use the new keyword to create an object using notepad and run that in cmd, it would give me an error ?

It's faster to do just that and get the answer for yourself than to post the question here and wait for someone else to give you the answer.

Stephen M. Webb
Professional Free Software Developer

Yes, without specifying one a Default no argument constructor does exist for classes, but if you define a constructor (like the one taking a name), then the no argument constructor can no longer be used unless you also define that.

It's one class with two constructors within it. One of those is an explicit default constructor. If you had provided no explicit constructors then an implicit default constructor would be generated for you by the compiler (but that's nothing to do with your IDE!).

Top-level classes don't need to be static and void is only for methods not classes. (Classes don't 'return values')

Advertisement

Thank you, methods are used for calculations and such and therefore return a value, so when a method is called are there parameters that say return the value into these variables ?

the variables are then used within the class for some reason or another.

This topic is closed to new replies.

Advertisement