0

I'm trying to create a LinearNode class with a non default contractor but passing the two arguments. I tried this but I'm getting an error. Any idea why?

public class LinearNode<T> (T elem, LinearNode<T> node){
        private LinearNode<T> next = node;
        private T element = elem;
    }

Thanks!

3 Answers 3

2

You have mixed the constructor with the class definition. The constructor is a special member function and should be defined more or less like a method (with no return type and the same name as the class).

public class LinearNode<T> {
    private LinearNode<T> next;
    private T element;

    LinearNode(T elem, LinearNode<T> node) {
        next = node;
        element = elem;
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

correct answer, but could use some explanation. This site is not meant to be a place to have people write code for you.
Ok, I thought the example was so small that it explained it self. But you are right.
1

You can't have (arguments) with class declaration

And also

you can't specify access specifier private for local variables

Comments

0

Your constructor declaration needs to be separate from your class declaration. Like so:

public class LinearNode<T>{
    private LinearNode<T> next;
    private T element;
    LinearNode<T>(T elem, LinearNode<T> node){
        next = node;
        element = elem;
   }
}

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.