Sign in to follow this  
Xer0botXer0

Help Identifying lines of code

Recommended Posts

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. 

 

Share this post


Link to post
Share on other sites

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.

Edited by GibbonThatCodes

Share this post


Link to post
Share on other sites

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 ? 

Edited by Xer0botXer0

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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')  

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this