This question already has answers here:
What is the meaning of "this" in Java?
(22 answers)
Closed 4 years ago.
public class MyResults extends Results {
...public MyResults() {
this(5);
}
public double average() {
return this.getSum()/numberOfCourses;
}
}
What do both instances of ―this mean in the code?
First instance is a call to another constructor in the same class. This is also known as Constructor Chaining pattern. Since you didn't post the entire code we don't know if that other constructor is defined (it should be, otherwise you'll have a compile time error).
Second instance is a call to the getSum() method. This method might be defined either in MyResults class or Results class (or some parent class of Results, if any).
Related
This question already has answers here:
What is the meaning of "this" in Java?
(22 answers)
Closed 3 years ago.
For example object1 has an instance method1:
public void method1() {
this.object2.method2(this);
}
Would the first 'this' refer to the object 1 and the second 'this' object 2 ?
In your code the this keyword give the method a sense of which variable, object or instance it will call, while this can also call the parents variable, object, instance or method. So when it calls this.object2.method2(this) it actually calling for the object2's method2 with a parameter of the class.
In your scope there is a code that instantiate the object2 and it is declared outside any method of the class. See more in here: use of this (javadocs)
This question already has answers here:
Why do we need a default no argument constructor in Java?
(7 answers)
Closed 5 years ago.
Why would anyone want to define a zero argument constructor when a default parameterless constructor is created during compile time anyway? Are there any benefits to doing so? are there any cases where it is better to define a zero argument constructor within a class for the sole purpose of preventing the default from being created during compile time?
Simple: you define it when you need it.
For example to call another constructor with some default values which are then used by the other constructor to initialize fields of the class.
When you write down an "empty" constructor that does nothing besides calling the super constructor then sure - you wrote useless code that should go away.
A default constructor is added by java internally if user does not define any parametrised constructor. It initialises any uninitialised fields to their default values.
Like int to 0
string to null
object to null
It's needed if you want to execute an init-function as soon as a new object of the class is created. For Example:
public class Example {
public Example() {
init();
}
public void init() {
//do some Stuff
}
}
But Just defining it without doing anything is not sensefull.
This question already has answers here:
Why doesn't Java allow overriding of static methods?
(22 answers)
Closed 5 years ago.
class Abc{
public static void hello(){
System.out.println("parent");//Line1
}
}
class Abc1 extends Abc{
public void hello(){//Line2
System.out.println("child");//Line3
}
}
Compiler gives error at line 3 saying that
This instance method cannot override the static method from Abc
Why can static methods not be overridden by a instance method?
Simple: because the language specification says so.
That is one of the downsides of static methods: there is no polymorphism for them! Conceptually, they are not meant to be overridden. That is all there is to this.
To be precise: the JLS says differentiates between static and non-static method and states:
An instance method is always invoked with respect to an object, which becomes the current object to which the keywords this and super refer during execution of the method body.
This question already has answers here:
Curly braces in "new" expression? (e.g. "new MyClass() { ... }")
(3 answers)
Closed 6 years ago.
Animal is a user defined class here.
Animal D = new Animal("Leo") {
#Override public void makeNoise() {
System.out.println("Roar!");
}
}; D.makeNoise();
Its called an anonymous class and used to define the class and any overridden methods at the same time.
That is an anonymous class. For details about anonymous classes and why they are useful, see this tutorial on anonymous classes.
This is used to override the initial
Animal.makeNoise()
method with a custom one for just this instance.
This question already has answers here:
Use of TypeLiteral in java
(5 answers)
Closed 9 years ago.
There is an ASTNode subclass called TypeLiteral.
The description of this type of code says that it looks like
( Type | void ) . class
I don't believe I have ever seen anything like this.
Can anyone provide an example of a TypeLiteral, and perhaps additionally of it being used in a Java program?
There is a class called Class that represents the class of an object. It contains various useful methods for introspection on that class and objects of that class. One way to get a class object is to use the Object method getClass(). But if you want to refer to a Class by name of its class, you use a class literal.
Some examples:
class Foo {
void method(Object b) {
if(b.getClass() == Foo.class) {
Foo f = Foo.class.cast(b);
}
Foo.class.getResourceAsStream(...);
Foo.class.getMethod("method", Object.class);
}
}