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.
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:
What is the use of an empty abstract class?
(3 answers)
Closed 5 years ago.
Yesterday, I had an interviewing with a company.
They ask me "Why do we need an empty class in Java?"
I never use an empty class so I have no idea about the purpose of this.
Anyone help?
Thank you so much.
No, we don't need an empty class in Java. We can create one but there is literally no benefit of having that empty class.
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.
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:
Casting variables in Java
(5 answers)
Downcasting in Java
(12 answers)
Closed 6 years ago.
As the title,
Assume that there's a KiahApplication class that extends Application,
I've seen the code below:
KiahApplication Kapp=(KiahApplication)getApplication();
But isn't it incorrect to cast a superclass to its subclass?
Thank you so much for helping!
No, it's not illegal and sometimes is necessary. The important thing is to be sure that the object is an instance of the subclass. You can do this by testing it in code or by catching the potential ClassCastException. In the example you give, if you are absolutely sure that the Application object is of the right type then you could leave as is.