This question already has answers here:
What is the meaning of "this" in Java?
(22 answers)
Closed 5 years ago.
I saw multiple use of the keyword this in advanced java programs and in android java files. Can anybody please explain to me the use of this? So I can understand the programming better.
As said by Oracle's Java documentation
:
Within an instance method or a constructor, this is a reference to the current object — the object whose method or constructor is being called. You can refer to any member of the current object from within an instance method or a constructor by using this.
I would check out the documentation as the this keyword is used often in programming.
Related
This question already has answers here:
How to get the MethodInfo of a Java 8 method reference?
(11 answers)
How to get string name of a method in java?
(7 answers)
Closed 5 years ago.
So if I were logging and wanted to reference a class, it might not be a bad idea to use something like this:
log.warn("I did not find an instance of class "+SomeClass.class.getName());
This way a refactor would be guaranteed to update the string to use the correct name.
It would be really neat to be able to do this with Methods. You can't with Java 7, but perhaps something based on java 8 would work, like this:
log.warn("You forgot to call "+(SomeClass::aMethod).getName()+" before calling this method");
--it WOULD work great if SomeClass::aMethod was actually a method reference and not a Lambda.
Does anyone know of a way to make this work, perhaps extract the original method name from the lambda?
This question already has answers here:
Printing all variables value from a class
(9 answers)
Closed 6 years ago.
Coming from JavaScript, I find it surprisingly hard to do so. In JS it was as easy as writing object's name in console. One way is to override toString(). But it is too tedious since I have to print all the instance variable separately? Am I right? If yes, is there another way?
Yes you right, but you can use some tools for override toString method easily, for example ToStringBuilder method reflectionToString
This question already has answers here:
Is it possible to create an instance of an object in Java without calling the constructor?
(4 answers)
Instantiate objects without using new operator
(8 answers)
Closed 7 years ago.
Normally a constructor will execute when will we create a object for a class. but my question, is it possible to create a object with out executing a constructor?
Without any byte code manipulation and dirty hacks, no matter what, your constructor always executes on Object creation. If you do not pass any args, it will surely invoke your default constructor.
You are wanting to live in a home, without building it ;)
This question already has answers here:
What is Implicit constructors on Java
(2 answers)
Closed 7 years ago.
In what situation we implement the explicit constructors in java program and when we doesn't need to implement explicit constructors.
This article explains the full story. In short: you need explicit constructors if you want to pass parameters to this constructor. If you do not need this, you can either provide a "parameterless" constructor ( public Foo() {} ), or you do nothing, in which case the system will create an empty one for you in the background.
This question already has answers here:
Static method in Java can be accessed using object instance [duplicate]
(5 answers)
Closed 10 years ago.
I know this would be too basic question but still I requset you guys to answer. As we all know that we can call a static method with object. I am not sure why someone would like to do this. Can someone please explain me a situation where this would be needed?
If this is not needed then why java dont give an error instead of warning for calling a static method using object.
Can someone please explain me a situation where this would be needed?
Let say you have a class named Container. This container will have a method ,say convertOunceToLiter(param). We can use this method even if have not yet make a container instance as a conversion method in some other places.So this method should be defined as static.