Declaring Methods  «Prev  Next»


Lesson 4 Working with objects
ObjectiveDescribe how Objects are referenced.

Creating and using Objects in Java

An object is typically created using a constructor and the new operator.
ClassName variable = new ClassName(argumentList);

If ClassName has a field variable named field1 and a method named method1(), then field1 may be accessed as variable.field1 and method1() may be invoked as variable.method1(argumentList).

Using 'this' and 'super'

Java provides the this keyword to refer to the current object instance.[1] The this keyword is used within constructors and non-static methods. When this is used within a constructor, it refers to the object being created. When it is used within a non-static method, it refers to the object whose method is being invoked. Since static methods are not associated with an instance of a class, you should not use this within a static method.
The super keyword is similar to this, except that it refers to the superclass of the current object instance. The super keyword is used to access field variables and methods of an object's superclass that may have been hidden or overridden by those of the object's class.


this and super
this and super


Benefits of passing References to methods in Java

In Java, the distinction between passing primitive data types and reference data types to methods is a foundational concept that has significant implications for program efficiency, design, and functionality. The practice of passing references, in particular, offers a range of benefits that enhance the robustness and adaptability of Java programs. This analysis delves into the myriad advantages of passing references to methods in Java and their impact on software development.
  1. Memory Efficiency: When a reference to an object is passed to a method, only the memory address of the object is passed, rather than a copy of the entire object. This approach is considerably more memory-efficient, especially when dealing with large objects, as it circumvents the need to allocate additional memory for object duplicates.
    • Conservation of Memory: Passing references avoids the redundant allocation of memory, thereby conserving system resources.
  2. Mutable State Preservation: Passing references allows methods to modify the state of the passed objects directly. This capability is pivotal when operations necessitate the alteration of object attributes, ensuring that changes are persistent and reflected across the entire program.
    • In-Place Modification: Changes made to the object within the method are propagated to the original object, ensuring data consistency.
  3. Enhanced Performance: Owing to the avoidance of copying large objects, passing references can lead to faster execution times. The system spends less time on memory allocation and data copying, thereby accelerating method invocation and data manipulation processes.
    • Reduced Overhead: Minimized data copying translates to quicker method calls and overall improved program performance.
  4. Facilitation of Object Interactions: Passing references enables methods to interact with multiple objects simultaneously. This is particularly beneficial in scenarios where relationships and interactions between objects dictate program logic and flow.
    • Inter-Object Communication: Methods can mediate and manage interactions between multiple objects, fostering complex data manipulations and operations.
  5. Augmented Data Abstraction: By passing references, methods can operate on objects without needing explicit knowledge of their internal structure or representation. This promotes the principle of data abstraction, where the implementation details are concealed, and only the essential features are exposed.
    • Encapsulation Enhancement: Data abstraction facilitated by passing references bolsters the encapsulation principle, a cornerstone of object-oriented programming.
  6. Flexibility in Method Design: Methods that accept references can be designed to operate on a wide array of objects, provided they conform to a specific interface or superclass. This polymorphic behavior enhances method reusability and adaptability.
    • Polymorphism Promotion: Passing references enables methods to exhibit polymorphic behavior, allowing them to operate on diverse object types.
The practice of passing references to methods in Java is not merely a syntactic choice but a strategic decision that offers a plethora of benefits. From conserving memory and enhancing performance to promoting core object-oriented principles like encapsulation and polymorphism, the advantages are both profound and multifaceted. As Java continues to be a dominant force in the software development realm, understanding the nuances and benefits of passing references remains imperative for developers aiming to craft efficient, scalable, and robust applications.

The Instance program illustrates the use of this and super.
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.

Java-instance program

The Instance program illustrates the use of this and super. The Instance constructor invokes the display() method in the following ways:

display(this.s); 
display(super.s); 
this.display(s); 
super.display(s); 

This results in the following output being displayed:
this: this 
this: super 
this: this 
super: this                


The Instance program

class Instance extends SuperClass {
  String s = "this";

  public static void main(String[] args) {
    new Instance();
  }
  
  Instance() {
    display(this.s);
    display(super.s);
    this.display(s);
    super.display(s);
  }

  void display(String s) {
    System.out.println("this: " + s);
  }
}

class SuperClass {  
  String s = "super";

  void display(String s) {
    System.out.println("super: " + s);
  }
}

[1]Current object instance: The current object that is being executed.