1

I could not understand the following concept of how it works when we create the class object

class MyClass {
    int data;
    MyClass var;  
}

I'm totally confused about how it will work and what is the purpose of creating the object of the same class name. I can't understand what is the concept of this declaration.

lealceldeiro
  • 14,342
  • 6
  • 49
  • 80
  • 2
    Are you familiar with a linked list? The concept is practically identical. – Joe C Oct 26 '18 at 18:59
  • 3
    In real life: you are a Person. And you have a father. Your father is also a Person. Replace MyClass with Person in your example, and var with father, and you have this real-life situation modeled as a class. – JB Nizet Oct 26 '18 at 19:01
  • yes but how it work as i mentioned method name with same as class name called constructor but what this type of declaration called ??? – user3116344 Oct 26 '18 at 19:02
  • 3
    You have no method in the code you posted, and no constructor either. So I'm not sure what you're talking about. What you have is a class, with two fields (or instance variables). https://docs.oracle.com/javase/tutorial/java/javaOO/variables.html – JB Nizet Oct 26 '18 at 19:04
  • Chicken-and-egg dilemma, isn't it? –  Oct 26 '18 at 19:04

3 Answers3

3

This is a completely valid Java class declaration. An example of what MyClass var; means in your code is this one:

Imagine this class is called Directory (it represents a directory location in a hard drive in real life) and we want to simulate that a directory can have more (sub) directories (for simplicity, only one :) ). So we could simulate this using this structure:

class Directory {
    int amountOfFolders;
    Directory subdiretory;         // <-- child object(directory)!
    // Directory[] subdiretories;  // out of curiosity, this would be much more real
}

This way when we create a Directory object and set its child directory(ies) and do some stuff with it.

This is called Composition (read more at Difference between Inheritance and Composition and Inheritance and Composition (the "Composition’s Basics" part) )

See below code commented for some more details...

class MyClass {

    int data;
    // attribute of type MyClass, this can represent a hierarchical structure
    // in real life applications.
    MyClass var;

   // this is not a class method, but its constructor, used to create instances
   // of this class, i.e.: MyClass c = new MyClass();
   public MyClass() {
   }

   // this is another constructor, which receives a parameter for setting its
   // attribute data, i.e.: MyClass c = new MyClass(3);
   public MyClass(int data) {
       this.data = data;
   }
}

Demo: https://ideone.com/2qirCQ

Here is the GitHub repository with this code.

lealceldeiro
  • 14,342
  • 6
  • 49
  • 80
2

When you want to model some real world scenarios, you might have to use this notion.

For example, think of a Tree's branch. A tree's branch might have n number of branches on it. A branch will have reference to the branches next to it.

class Branch {
    Branch branch1;
    Branch branch2;
    Branch branch3;
}
Alon
  • 687
  • 1
  • 6
  • 10
1

In your case, each instance of MyClass can have a reference to another instance of MyClass. The name doesn't give much of a hint as to why you would want it. But in general this kind of structure allows linking, decorating and nesting.

For example, you could implement a chain of processing steps. Each one calls the next one in the chain until the end (var == null).

Alternatively, your class could "wrap" another instance of the class and do something like log method calls before delegating them to the wrapped instance.

A similar structure can be used for modeling trees as a Node that each has a list of child nodes (List<Node> children). The leaf nodes would have no children.

ᴇʟᴇvᴀтᴇ
  • 12,285
  • 4
  • 43
  • 66