what’s the difference between these two pieces of code?
I was experimenting with these two pieces to see if they still have the same output and I thought they would but I keep running into an error for the replit one (the one with the dark mode bg)
The one in the light mode bg is the original piece of code and I tried to create the same output differently in the one with the dark mode bg but idk what is wrong
Can anyone pls explain/help me
47 Replies
⌛
This post has been reserved for your question.
Hey @applesandhoney! Please useTIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here./close
or theClose Post
button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
The output for the light mode bg is supposed to be 30
And that’s what im trying to get in the dark mode bg code too but idk what is wrong
Unknown User•12mo ago
Message Not Public
Sign In & Join Server To View
There is no
this.n
that is part of the Overtime
class
only n
since it is the argument of the constructor
Other than that I think it's pretty much accurateSo I just write
Overtime ot = new Overtime(n); ?
But doesn’t
Overtime ot = new Overtime(this.n);
Basically just pass a reference of the parameter variable n? So isn’t it basically the same thing then?
no
this.n
is an instance variable call
there is no instance variable called n
only an argument that is only accessible within the method it`s an argument of
see
./run
Here is your java(15.0.2) output @noComment | Kez
ohh okay
wait so in the code with the light mode bg,
Overtime ot = new Overtime(this)
does it just take the entire object that is passed to it?
this
in this context passes the current Pay
object into the constructor of Overtime
an object is an instance of a class
it is and since its
one.calculatePayWithOverTime
it passes one into overtime later
so it depends on which object we call the calculatePayWithOverTime
then right,Well I don't get why you gave
calculatePayWithOverTime
an argument anyways
just delete the argument
make the call Overtime ot = new Overtime(this);
and be done with iti did it cause i thought it'd be the same as
Overtime ot = new Overtime(this);
but just that its passing the object as a parameter variable?? im not sure if i make sense
but got it, this.
is just an instance variable callwell it is the same as long as you pass the same object into it
can also be an instance method call
and
this
without the .
just represents the instanceso isnt
Overtime ot = new Overtime(this.n);
passing the object? or not cause like u said n is a parameter and not instance
also if i did just Overtime ot = new Overtime(n)
would it mean the same thing?this.n
wont work here because there is no instance variable called n
okay
and making it
Overtime ot = new Overtime(n)
kind of defeats the purpose of the methodoh cause then i can just call overtime from main
The method is supposed to act upon the instance it is called from
okay
so we call
this
not n
the method is not supposed to have an argument
why would you do that?no like it was in response to how u said it defeats the purpose of the method, like thats why it defeats the purpose?
it defeats the purpose because you could just pass another pay object
which wouldnt make sense
okay
doesn't really make sense right?
yeah
it doesnt
exactly
public class Pay
This message has been formatted automatically. You can disable this using
/preferences
.it gives me 30 now
sry ik i made it extra complicated but i just wanted to see if there was any other way to get 30 using this
are you trying to make fun of me
no
i just was trying to get another way
okay
sry i didnt mean it that way
yeah I was a bit harsh sorry
i shared cause i wanted to ask that in the real world would this still be acceptable or not
cause ik u said last time that theres a style of writing code
well not really
oh
okay yeah im just overcomplicating it
since you still have this issue
ohh
the method is very clearly not intended to have an argument
and since i call the method on the
one
object, it should just pass the one
object?exactly
in this case at least
since it is a method that is intended to alter the instance from which it is called
okay
anything else I can help you with?
nope thats good
ty for ur help
also sry abt that, i didnt intend to come off as rude or anything
Nah I just misinterpretet you are in the right
If you don't need further help you can just close this post 🙂
Post Closed
This post has been closed by <@1127841130252357733>.