This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin for Android App Development and have Peter Sommerhoff on-line!
See this thread for details.
Win a copy of Kotlin for Android App Development this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

"this" keyword reference  RSS feed

 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

//output "Daniel"
//output "Daniel"
is these outputs are correct. or whether the first output will be "hello".
this class will let me to understand the "this" keyword forever.
and another question, is it possible to write the constructor name only as instead of ?
thank you guys.
 
author & internet detective
Posts: 39129
723
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

rian bron wrote:
//output "Daniel"
//output "Daniel"
is these outputs are correct. or whether the first output will be "hello".


They will both be "hello" because you are calling the "get" method before anything else happens.

rian bron wrote:
and another question, is it possible to write the constructor name only as instead of ?


In this case, you can. When you remove the "public", the access type is package-private. This means you can use it in that class or package.
 
Marshal
Posts: 6510
441
BSD Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi rian,

Did you try to run this code and analyse what happens? Always try to execute the code and see what happens.
 
rian bron
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:Hi rian,

Did you try to run this code and analyse what happens? Always try to execute the code and see what happens.



it doesn't let me run it :
running-problem.jpg
[Thumbnail for running-problem.jpg]
 
rian bron
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jeanne Boyarsky wrote:

rian bron wrote:
//output "Daniel"
//output "Daniel"
is these outputs are correct. or whether the first output will be "hello".


They will both be "hello" because you are calling the "get" method before anything else happens.

rian bron wrote:
and another question, is it possible to write the constructor name only as instead of ?


In this case, you can. When you remove the "public", the access type is package-private. This means you can use it in that class or package.




does line 3 changes the value of the _name of the private _name variable?
 
Liutauras Vilda
Marshal
Posts: 6510
441
BSD Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't remember how to use BlueJ. Try to read how to compile and execute programs from command line, that is more trusted way at the beginning.
 
rian bron
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:I don't remember how to use BlueJ. Try to read how to compile and execute programs from command line, that is more trusted way at the beginning.



ok i compiled it, it did give me the output of "hello" twice.
do you have example of creating 2 variables with the same and changing one of  them and referring to the original one?
like i tried ?
 
Liutauras Vilda
Marshal
Posts: 6510
441
BSD Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm sorry, but I couldn't understand your question/request.
 
rian bron
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:I'm sorry, but I couldn't understand your question/request.


i will try to explain by myself and would like you to correct me if i'm wrong:
we use the "this" keyword to referred to the local variable-(the variable which is the attribute of an object)
lets take this example:



Please Tell me if my output answers will be true if will run it.
i know that i can just check it by myself and check whether it works or not, but i want to understand the principal behind the "this" keyword, and not this specific question.
thank you
 
Saloon Keeper
Posts: 5485
55
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"this" does not refer to a "local" variable.
"this" refers to an instance variable, aka "field", aka "member variable".
A "local" variable has a scope limited to the block that contains it. Once the block exits a local variable disappears though it may persist if a reference to it has been copied to a variable outside the code block.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!