File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Passing enum value to a constructor. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Passing enum value to a constructor." Watch "Passing enum value to a constructor." New topic
Author

Passing enum value to a constructor.

Niccola LeBaron
Greenhorn

Joined: Apr 15, 2011
Posts: 4


The last item is supposed to be passed as an ENUM value and it doesn't like it.

The constructor I am passing to is like this:


The ENUM is like this:


I have looked online and tried the formatting examples but none are working. How do I get the ENUM value SUV to pass to my constructor?
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3434
    
  47

You need to specify the enum class along with the enum constant:

It does not actually matter that you're passing it to a constructor. This must be done practically wherever enum constant is used.
Niccola LeBaron
Greenhorn

Joined: Apr 15, 2011
Posts: 4
Telling me AutoType can't be resolved. I have seen examples without the period. What is the period doing? When would a period be correct and why would examples show a space? Does it have something to do with the method being in the same class or being in an instanceof? I hope I am using the right verbiage.
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3434
    
  47

Niccola LeBaron wrote:Telling me AutoType can't be resolved.

You've probably got more than one package in your project. If so, you need to import the AutoType enum into the source file using the import statement. Read this tutorial on packages and come back if anything is not clear.

I have seen examples without the period. What is the period doing? When would a period be correct and why would examples show a space?

The period serves to reference a field that is part of some class or instance, or (alternatively) to separate class name from package specification. In your case, the SUV constant is part of the AutoType class (enum is just a little bit special class in Java - find out and read Java tutorials on enums if you're not sure why SUV is a constant or how is it part of the AutoType class).

If you use enum in a switch statement, you don't have to specify the enum class in individual case statements inside the switch - the class is unambiguously determined by the type of the expression in the switch statement.

There is a way to avoid the need to specify class name in other statements too (the import static statement). However, I'd suggest you to ignore it now and revisit it once you get better grasp of the more basic Java concepts.

Does it have something to do with the method being in the same class or being in an instanceof?

Yes. Statements in a method of a class do not have to qualify the same class or its super classes with name - it is inferred automatically from the context.

The instanceof is somewhat connected to this concept as it tests whether an object is an instance of a class, an instance of a subclass, or an instance of a class that implements a particular interface.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Passing enum value to a constructor.
 
Similar Threads
proble with adding jscrollpane to jframe
Need help with Car Inventory Program part 3
Key Advantages of Java 5
Messy enum problem
Exception in thread "main" java.lang.NullPointerException error