Win a copy of Production-Ready Serverless (Operational Best Practices) this week in the Cloud/Virtualization 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
  • Bear Bibeault
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

"this" keyword reference  RSS feed

 
Ranch Hand
Posts: 101
  • 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: 39195
725
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: 6634
443
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: 101
  • 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: 101
  • 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: 6634
443
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: 101
  • 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: 6634
443
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: 101
  • 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: 5753
56
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.
 
It is an experimental device that will make my mind that most powerful force on earth! More powerful than this tiny ad!
global solutions you can do in your home or backyard
https://coderanch.com/t/708587/global-solutions-home-backyard
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!