instantiating object using interface and object file

Posted on 2012-09-20
Last Modified: 2012-09-21
I have a VideoObj object:


 * Implementation of Video interface.

 * @see Data


final class VideoObj implements Video {

  private final String _title;

  private final int    _year;

  private final String _director;


   * Initialize all object attributes.


  VideoObj(String title, int year, String director) {

    _title = title;

    _director = director;

    _year = year;


  public String director() {

    // TODO

    return _director;


  public String title() {

    // TODO

    return _title;


  public int year() {

    // TODO

    return _year;


Open in new window

This object implements a Video interface.  I am tasked to finish a factory method in a separate class called Data.  

Data class (factory method):

   * Factory method for Video objects.

   * Title and director are "trimmed" to remove leading and final space.

   * @throws IllegalArgumentException if Video invariant violated.


  static public Video newVideo(String title, int year, String director) {

    if (  (title == null)

 	       || (director == null)

 	       || (year <= 1800)

 	       || (year >= 5000)) {

      throw new IllegalArgumentException();


    title = title.trim();

    director = director.trim();


    if (  ("".equals(title))

 	       || ("".equals(director))) {

      throw new IllegalArgumentException();


    return newVideo;


Open in new window

My IDE is telling me the year = year is

"Multiple markers at this line

- The method year() is undefined for the type


- The assignment to variable year has no effect"

Also it is complaining on return newVideo:

"newVideo cannot be resolved to a


I am pretty lost and very rusty on java.  I am most confused about accessing private variables (title, year, etc are private in VideoObj).  Also, I am not sure what to return.  Any guidance /advice?
Question by:dazedandconfused69
    LVL 35

    Accepted Solution

    Is this for a homework assignment? If so, you should state that in your question so that the experts on here can give the right level of help, ie. not just give you full code solutions, and actually try and help you to write your own code. For the moment, I will assume that it IS homework, and just provide some pointers...

    The method year() is undefined for the type
    This would have been from line 29 of above (this will be a different line in your code, you should really post your whole class if you can to give us the right context). The method call on this line would be trying to call a method named 'year' and it would need to be in the Data class itself. Am guessing that this isn't at all what you want and you can probably just remove this line.

    The assignment to variable year has no effect
    I'm guessing that you may have posted different code to what gave these errors, because I can't see where this error would be thrown from.

    newVideo cannot be resolved to a variable
    Basically, as it says, newVideo isn't a variable and so it can't work out what you want to return. The general idea of a factory method is that is creates an object, sometimes initialising it (as in your case) and returns that object to the caller. Hints: the 'new' keyword is what creates objects in Java, the VideoObj constructor is the only way you have of initialising the object so you will need to use it, and the object that is created is what you will 'return' to the caller of your factory method.

    I am most confused about accessing private variables (title, year, etc are private in VideoObj).
    Fear not then, as you don't need to access them. You pass in the values that you want the VideoObj to have via the constructor (line 25 of and the constructor sets up the private variables. Also, when you want to retrieve the values in those variables, again you don't access the private variables directly but via the 'getter' methods (line 37, 47 & 57 of Note: to conform to the general standard, you probably should change the names of those methods to getDirector(), getTitle() and getYear(), possibly in the Video interface too if they are part of that interface.

    Author Comment

    Thank you for the comments.  It is a HW assignment.  I will follow your advice going forward.  Is it appropriate for me to suggest a solution based on an Expert's advice and for that expert to confirm that solution is correct?
    LVL 35

    Expert Comment

    Yes, that is actually the preferred way. If you post your solution and ask specific questions about certain parts, we can definitely help. I mean, pretty much what you posted in the original question is good, it shows that you attempted something yourself, and you asked specific questions. I just said that stuff about HW assignments as some experts on here might have just replied with code only (no explanation) and that just doesn't really help you in the long run! As I said what you did was good, a lot of the time though we see other people just posting the HW question verbatim and hoping someone will code everything for them!

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
    Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
    This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now