Java difference between this and class this

Difference between this and super keywords in Java

this and super are two special keywords in Java, which is used to represent current instance of a class and it’s super class. Java Programmers often confused between them and not very familiar with there special properties, which is asked at various core Java interviews. A couple of questions, which I remember about this and super keyword is that, Can we reassign this in Java? and the difference between this and super keyword in Java. Do you want to try that? Ok, I am not giving the answer now, rather I will let you know the answer at the end of this post.

As I said in the first line, the main difference between this and super in Java is that this represents current instance of a class, while super represents the current instance of the parent class. Now where does this and super variables used, well you might have seen examples of calling one constructor from other i.e. constructor chaining, that’s achieved by using this and super keyword?

You can use this() to call no-argument constructor of same class, while super() to call no argument or default constructor of parent class. By the way, call is not limited to only no argument constructor, you can call any constructor by passing appropriate parameters. We will see example of using this and super in a while.

Another use of this and super in Java is for accessing instance variables of a class and it’s parent. By the way, you can also access them even without prefixing super and this , if they are not ambiguous in current block e.g. if there is no local variable of same name exists in that block, but in case of ambiguity they provide explicit reference and are more readable.

One of the classic example of using this is inside a constructor, which accepts a parameter of same name as an instance variable. In this post, we will learn about more differences between super and this in Java and take a look at some of the use cases.

Читайте также:  Java web application startup

Similarities between this and super keywords in Java

Before seeing difference between this and super keywords in Java, let’s see some similarities between them :

1. Both this and super are non static and can not be used in static context, which means you can not use this and super keyword inside main method in Java. Failing to do so will result in compiler error «non static variable this can not be referenced from static context» . Same is true for using super keyword inside main method.

public static void main(String args[])  // compiler error - non static variable can not be referenced from static context System.out.println(this.name); >

2. Both this and super can be used in constructor chaining to call another constructor e.g. this() and super() respectively calls no argument constructor of child and parent class.

As shown in this example, we are first forwarding call from no argument constructor of B, to a constructor which accepts one String argument, which further call to super(«») , a call to super class, one argument constructor.

 System.out.println("A's no argument constructor");
 System.out.println("A's one argument constructor");
class B extends A
this(""); // calling one arg constructor of class B 
 System.out.println("B's no argument constructor");
super(""); // calling one argument constructor of class A 
 System.out.println("B's one argument constructor");
 public static void main(String args[]) 
A's one argument constructor
B's one argument constructor
B's no argument constructor 

3) If used them inside constructor than this and super must be first statement, otherwise compiler will complain. Which means you can not call this() and super() from same constructor.

super vs this in Java

Now we know how to use super and this keyword in Java, and comfortable with their intended use. One use of this keyword, which I didn’t show here is that you can also use them inside Inner classes, they are quite handy to get the reference of the outer class in Java, Outer.this and Outer.super can be used to get the current instance of Outer class and it’s a parent in Java.

Don’t forget to replace Outer , with the name of the enclosing class. Now In short, here are the main differences between this and super keyword in Java

1. this is used in the context of the class you are working on, while super is used to refer current instance of the parent class.

2. Every constructor by default calls super() , which is a call to the no-argument constructor of the parent class, but you can still call another constructor either explicitly by using this() or super() .

difference between this and super keyword in Java

That’s all on the difference between this and super keyword in Java and How to use them in a Java program. We have seen calling another constructor from this and super keyword, and access member variables from current and parent class using them.

Remember they are very special variables, and now answer of my question, which I asked in the first paragraph. No, you can not assign a new value to this variable, because it’s final. You can try doing that in an IDE, you will get compiler error «can not assign a new value to final variable this».

Источник

What is the difference between Class.this and this in Java

There are two ways to reference the instance of a class within that class. For example:

class Person < String name; public void setName(String name) < this.name = name; > public void setName2(String name) < Person.this.name = name; > > 

One uses this.name to reference the object field, but the other uses className.this to reference the object field. What is the difference between these two references?

Java Solutions

Solution 1 — Java

In this case, they are the same. The Class.this syntax is useful when you have a non-static nested class that needs to refer to its outer class’s instance.

class Person< String name; public void setName(String name)< this.name = name; > class Displayer < String getPersonName( ) < return Person.this.name; > > > 

Solution 2 — Java

This syntax only becomes relevant when you have nested classes:

class Outer< String data = "Out!"; public class Inner< String data = "In!"; public String getOuterData()< return Outer.this.data; // will return "Out!" > > > 

Solution 3 — Java

You only need to use className.this for inner classes. If you’re not using them, don’t worry about it.

Solution 4 — Java

Class.this is useful to reference a not static OuterClass .

To instantiate a nonstatic InnerClass , you must first instantiate the OuterClass . Hence a nonstatic InnerClass will always have a reference of its OuterClass and all the fields and methods of OuterClass is available to the InnerClass .

public static void main(String[] args) < OuterClass outer_instance = new OuterClass(); OuterClass.InnerClass inner_instance1 = outer_instance.new InnerClass(); OuterClass.InnerClass inner_instance2 = outer_instance.new InnerClass(); . > 

In this example both Innerclass are instantiated from the same Outerclass hence they both have the same reference to the Outerclass .

Источник

Java difference between this and class this

  • Introduction to Java
  • The complete History of Java Programming Language
  • C++ vs Java vs Python
  • How to Download and Install Java for 64 bit machine?
  • Setting up the environment in Java
  • How to Download and Install Eclipse on Windows?
  • JDK in Java
  • How JVM Works – JVM Architecture?
  • Differences between JDK, JRE and JVM
  • Just In Time Compiler
  • Difference between JIT and JVM in Java
  • Difference between Byte Code and Machine Code
  • How is Java platform independent?
  • Decision Making in Java (if, if-else, switch, break, continue, jump)
  • Java if statement with Examples
  • Java if-else
  • Java if-else-if ladder with Examples
  • Loops in Java
  • For Loop in Java
  • Java while loop with Examples
  • Java do-while loop with Examples
  • For-each loop in Java
  • Continue Statement in Java
  • Break statement in Java
  • Usage of Break keyword in Java
  • return keyword in Java
  • Object Oriented Programming (OOPs) Concept in Java
  • Why Java is not a purely Object-Oriented Language?
  • Classes and Objects in Java
  • Naming Conventions in Java
  • Java Methods
  • Access Modifiers in Java
  • Java Constructors
  • Four Main Object Oriented Programming Concepts of Java
  • Inheritance in Java
  • Abstraction in Java
  • Encapsulation in Java
  • Polymorphism in Java
  • Interfaces in Java
  • ‘this’ reference in Java

Источник

Java difference between this and class this

Learn Latest Tutorials

Splunk tutorial

SPSS tutorial

Swagger tutorial

T-SQL tutorial

Tumblr tutorial

React tutorial

Regex tutorial

Reinforcement learning tutorial

R Programming tutorial

RxJS tutorial

React Native tutorial

Python Design Patterns

Python Pillow tutorial

Python Turtle tutorial

Keras tutorial

Preparation

Aptitude

Logical Reasoning

Verbal Ability

Company Interview Questions

Artificial Intelligence

AWS Tutorial

Selenium tutorial

Cloud Computing

Hadoop tutorial

ReactJS Tutorial

Data Science Tutorial

Angular 7 Tutorial

Blockchain Tutorial

Git Tutorial

Machine Learning Tutorial

DevOps Tutorial

B.Tech / MCA

DBMS tutorial

Data Structures tutorial

DAA tutorial

Operating System

Computer Network tutorial

Compiler Design tutorial

Computer Organization and Architecture

Discrete Mathematics Tutorial

Ethical Hacking

Computer Graphics Tutorial

Software Engineering

html tutorial

Cyber Security tutorial

Automata Tutorial

C Language tutorial

C++ tutorial

Java tutorial

.Net Framework tutorial

Python tutorial

List of Programs

Control Systems tutorial

Data Mining Tutorial

Data Warehouse Tutorial

Javatpoint Services

JavaTpoint offers too many high quality services. Mail us on h[email protected], to get more information about given services.

  • Website Designing
  • Website Development
  • Java Development
  • PHP Development
  • WordPress
  • Graphic Designing
  • Logo
  • Digital Marketing
  • On Page and Off Page SEO
  • PPC
  • Content Development
  • Corporate Training
  • Classroom and Online Training
  • Data Entry

Training For College Campus

JavaTpoint offers college campus training on Core Java, Advance Java, .Net, Android, Hadoop, PHP, Web Technology and Python. Please mail your requirement at [email protected].
Duration: 1 week to 2 week

Like/Subscribe us for latest updates or newsletter RSS Feed Subscribe to Get Email Alerts Facebook Page Twitter Page YouTube Blog Page

Источник

Оцените статью