0

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) {



}

}
4
  • 1
    It's just a self-referential field of type hello named n. Commented Oct 27, 2016 at 2:43
  • Hi Andrew, I don't quite catch your drift. Can you expand what you meant a little further if you don't mind? Commented Oct 27, 2016 at 2:45
  • You made a field named n, of type hello. You can make a new hello object, and inside that hello object, it can set another hello object. Commented Oct 27, 2016 at 2:46
  • a class name that's used to declare a variable? what do you mean? you always need the class name to declare a variable. Commented Oct 27, 2016 at 2:46

4 Answers 4

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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;
        }
}

Comments

0

Creating variable using class name is called object.

hello n = new hello();

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

n.someMethod();

2 Comments

I know about hello n = new hello(). is that the same as... public hello n outside of the main?
good, then you can use that object to access variable and method of class. Like any non static method from static method
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.