Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java regarding <E> in public static <E> void

Tags:

java

Using Java: I didn't want to waste peoples time and post this here, but my googling-skills have failed me and I can't find the answer. I was looking through some supplied code and they used

public static <E> void printTree(TwoFourTree<E> tf)

(For reference we are converting from a Red-Black tree to a Two-Four Tree). When I first approached this problem I would use instead of and not even include in the initial method declaration of public static void. However I ran into issues, and throwing in this <E> solved all my problems, despite using <Integer> instead of <E> everywhere else.

So my question is, can someone please explain to me what exactly the <E> does in

public static <E> void
like image 268
user1789016 Avatar asked Oct 31 '12 15:10

user1789016


People also ask

What is public static void in Java?

public means that the method will be visible from classes in other packages. static means that the method is not attached to a specific instance, and it has no " this ". It is more or less a function. void is the return type. It means "this method returns nothing".

What is the difference between public static and void in Java?

public − This is the access specifier that states that the method can be accesses publically. static − Here, the object is not required to access static members. void − This states that the method doesn't return any value.

Why does Java start with public static void?

When java runtime starts, there is no object of the class present. That's why the main method has to be static so that JVM can load the class into memory and call the main method. If the main method won't be static, JVM would not be able to call it because there is no object of the class is present.

What does void mean in public static void?

static means that the method is associated with the class, not a specific instance (object) of that class. This means that you can call a static method without creating an object of the class. void means that the method has no return value.


1 Answers

This is a Java feature known as Generics.

Why would you want to use Generics? Well, the following gives one scenario where they are useful. Back in the "bad old days", if you wanted to have a class that could work with any objects you had to declare all it's interfaces in terms of objects, e.g.

public class MySuperList
{
    public void Add(object a) { }
}

However, this sucks, because you get no type-safety - you could write code like the following:

// Your code
MySuperList list = new MySuperList();
cars.Add(new Car("Ford Mondeo"));// Works
cars.Add(new Fish("Herring")); // Works - which is a shame as a Fish is not a car!

Generics fixes this, by allowing you to strongly-type your class, but in a generic manner. For example:

public class MySuperList<T>
{
    // Add method will only accept instances of type T.
    public void Add(T a) { }
}

// Your code
MySuperList<Cars> list = new MySuperList<Cars>();
cars.Add(new Car("Ford Mondeo"));// Works
cars.Add(new Fish("Herring")); // Compile-time error.
like image 149
RB. Avatar answered Oct 20 '22 21:10

RB.