#Java extra method?

1 messages ยท Page 1 of 1 (latest)

fallow parrot
#

Hey, so my teacher sent us some code and I'm trying to understand it but at the end there is something that I would say is extra (shouldn't be there) as it has nothing inside it. Does it have a purpose or maybe it's a typo from teacher? Its the last "public Contacto()" that is empty

hot abyssBOT
#

<@&987246399047479336> please have a look, thanks.

hot abyssBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

fallow parrot
#

`public Contacto(String nombre, String telefono) {
this.nombre = nombre;
this.telefono = telefono;
}

**public Contacto() {**
**}**

}`

#

...

#

the last part

timber bear
#

is 'Contacto' a class ?

plucky scroll
#

by default, if you don't define any constructors, all classes get a no-arg constructor
but if you define at least one, no new constructors are generated
so if you want to bring back the no-arg constructor, you have to define it manually

fallow parrot
#

yes

fallow parrot
plucky scroll
#

could you be more clear what needs to be addressed

hot abyssBOT
fallow parrot
#

I already have the toString defined

plucky scroll
#

you mean the two constructors?

fallow parrot
#

so the "public Contacto(..." makes no sense to me

fallow parrot
plucky scroll
#

do you know what a constructor is

#

maybe you have lectures on that topic

fallow parrot
#

bhut there is another one

#

or what looks to be another one?

#

in the code I sent

plucky scroll
#

do you know what they are used for

fallow parrot
#

to give the object the parameters I gave it right?

plucky scroll
#

when is the constructor called?

fallow parrot
#

when you need to "build" the object like with params so if I have public Character(String name, String weapon, String class) for example

#

right?

plucky scroll
#

it's called when you create an object using new

#

new Contacto(...)

#

if you only define a constructor with 2 arguments, then you must pass 2 arguments to your call to new

#

if you define no constructors, you get a default one with no arguments
but if you define at least one, that one is gone and needs to be enabled manually

#

therefore, it's added to the code

fallow parrot
#

So why do I have 2 constructors?

#

In the code I sent

sturdy spear
#

each constructor defines a way that u can make an object

#

u got one that has a name and telephonenumber, and one that has no data

fallow parrot
#

ah, so in my case, i can make an object with 2 parameters or with none

sturdy spear
#

yes

fallow parrot
#

now I get it

#

thx

#

For the constructor to properly work you need to first define methods for the arguments right?

sturdy spear
#

yes / no. Your answer is a bit dubious due to miss use of terminology. Inside the constructor u can have stuff happening, or u can ( as u say ) use methods to do things, like setters and alike

timber bear
#

if the example says that u should put 2 values , then you get the constructor with 2 parametres that you created , otherwise takes the constructor without parametres ( default constructor) .Hope i was clear ๐Ÿ™‚

#

@fallow parrot

fallow parrot
#

yu thanks :D

#

I put in both just to be safe haha

#

but Im happy I was able to understand why

timber bear
#

good ๐Ÿ˜„