Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the compile time type of this? (in Java)

I suspect it is the type of the class in which it is written but I am not 100% sure, could someone please confirm my suspicion and perhaps give a reference to Java Language Specification where this behaviour is defined?

Let's say class A has a method a() which uses the this keyword in its body, and class B extends class A. Now class B has inherited method a(), however, I am not sure if the compile time type of this in B.a() is now A or B ?

I am asking this because I am trying to understand how the visitor pattern works, as it is described in this Robert C. Martin's Visitor chapter from The Principles, Patterns, and Practices of Agile Software Development.

It seems to be crucial to know the compile time type of this if one wants to fully understand the visitor pattern because overloaded method calls are resolved at compile time. More specifically, I refer to the compile time type of this in the accept methods in the visitor pattern.

like image 389
jhegedus Avatar asked Feb 12 '14 13:02

jhegedus


People also ask

What is compile time type?

The declared type or compile-time type of a variable is the type that is used in the declaration. The run-time type or actual type is the class that actually creates the object.

What is compile time in Java?

Compile time is the period when the programming code (such as C#, Java, C, Python) is converted to the machine code (i.e. binary code). Runtime is the period of time when a program is running and generally occurs after compile time.

What is runtime type in Java?

Runtime Type Identification in Java can be defined as determining the type of an object at runtime. It is extremely essential to determine the type for a method that accepts the parameter of type java.

What is compile time type checking?

Compile-time checking occurs during the compile time. Compile time errors are error occurred due to typing mistake, if we do not follow the proper syntax and semantics of any programming language then compile time errors are thrown by the compiler.

What is a compile time constant in Java?

Compile-Time Constants A Java variable is a compile-time constant if it's of a primitive type or String, declared final, initialized within its declaration, and with a constant expression. Strings are a special case on top of the primitive types because they are immutable and live in a String pool.

What are compile time errors in Java?

The Errors that occur due to incorrect syntax are known as compile-time errors or sometimes also referred to as syntax errors in java. Examples of compile-time errors include: missing parenthesis, missing a semicolon, utilizing undeclared variables, etc.

What is the difference between runtime and compile time in Java?

Difference between runtime and compile time Compile time is a process in which java compiler compiles the java program and generates a.class file. In other way, in compile time java source code (.java file) is converted in to.class file using java compiler.

How does a Java compiler work?

At compile time, the java compiler (javac) takes the source code (.java file) and checks if there is any syntax, type-checking or any semantic errors inside the program. If there is no error, the compiler generates a .class (bytecode) file for that .java file.


Video Answer


2 Answers

The type of this is the type of the class in which it is used. In fact, it is crucial for the visitor pattern from the article to work.

Visitor pattern implements double dispatch in two steps - selecting the appropriate accept method in the object being visited (first leg), and then selecting the appropriate visit method in the visitor (second leg). The first leg is implemented through overriding; the second leg is implemented through overloading.

Note that it is not necessary to use overloading for the second leg. In fact, it is common not to use it there for better readability. Compare these two implementations:

// Copied from Listing 29-2
public interface ModemVisitorOverload
{
    void visit(HayesModem modem);
    void visit(ZoomModem modem);
    void visit(ErnieModem modem);
}

public interface ModemVisitorNoOverload
{
    void visitHayes(HayesModem modem);
    void visitZoom(ZoomModem modem);
    void visitErnie(ErnieModem modem);
}

The second implementation is not using the overloading. It works in exactly the same way, except human readers of the code immediately see what is going on.

like image 193
Sergey Kalinichenko Avatar answered Sep 22 '22 14:09

Sergey Kalinichenko


this is a reference to the current object and as the JLS 15.8.3 states

The type of this is the class C within which the keyword this occurs.

At run time, the class of the actual object referred to may be the class C or any subclass of C.

like image 35
René Link Avatar answered Sep 20 '22 14:09

René Link