pellepelle
pellepelle

Reputation: 27

class variable same as class name java

I'm fairly new to Java & because I'm curious, I decided to declare a variable using my class name to see if something would happen. I get no error! Why's that so? What can you possibly do with a class name that's used to declare a variable? I've tried a few things but none seem to work.

Can anyone give me a brief explanation or an example possibly of what I can use my n variable for throughout my code?

Thanks and sorry if this is a nooby question :)

import java.util.*;

public class hello {
public hello n;

public static void main(String[] args) {



}

}

Upvotes: 0

Views: 3964

Answers (4)

Khuzi
Khuzi

Reputation: 2912

You declare any class and then you can create its instance inside the same class or outside in other classes(subject to class access modifier). And the reason you declare variable n of class Hello is because you can leverage the functionality of class Hello.

There are examples where you would have to create class instance in the same class itself like implementing data structures Linked lists, Queues etc.

e.g: Linked list implementation

private class Node {
        // reference to the next node in the chain, or null if there isn't one.
        Node next;

        // data carried by this node. could be of any type you need.
        Object data;

        // Node constructor
        public Node(Object dataValue) {
            next = null;
            data = dataValue;
        }
}

Upvotes: 1

castletheperson
castletheperson

Reputation: 33466

One extremely common example of a self-referential class is the Node of a LinkedList:

public class Node<E> {

    E value;
    Node<E> previous;
    Node<E> next;

}

Each Node references two other nodes which form the list structure of the LinkedList. The list ends when a referenced node is null.

This kind of structure is very powerful, and can be used in a huge variety of situations, such as in data structures like trees or graphs, or to show relationships between objects, like how a Person could have a List<Person> to represent the person's friends.

Upvotes: 1

Moonstruck
Moonstruck

Reputation: 503

A variable in java can have a name and a type. A type can possibly be a primitive type or a user define type. The one that you declared

public hello n;

is a variable of user defined type hello. In java, you can create your own type by creating a class (there are some advance stuff like adt,etc) but the basic way is using a class. Now, creating a variable of user defined type is called as creating an object of the class. Once, you create an object, you can give it all the functionality you want by listing them in the class. By creating the object of same class within a class, you are saying that I was an instance of "hello" type in hello itself. This stuff is more useful while creating data structures like linkedlist. for example, In a linkedlist, you can define a node of the linkedlist as

public class LinkedNode<E> {
    <E> data;
    LinkedNode next;
}

See, I just created same stuff as you did with hello n. Here, you are saying that the object of type LinkedNode should have two things- a data and a reference to the next node. Same is the case with your code, creating a field of type hello, you are saying that any instance of type hello should have an hello object in it. You can add methods and other functionality to the class as well.

Upvotes: 0

Sanjeet
Sanjeet

Reputation: 2403

Creating variable using class name is called object.

hello n = new hello();

It can be used to call methods, members of class.

n.someMethod();

Upvotes: 0

Related Questions