Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Creating an object through a class parameters

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Location
    Boston, Ma
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Creating an object through a class parameters

    I'm trying to figure out how to create a class through a constructor's parameters here is my code:

    public class Article {
      private String title;
      private String body;
      private ArrayList <Author> authors = new ArrayList<Author>(); // this isn't how I'm supposed to create the object
      private GregorianCalendar pubDate = new GregorianCalendar();  // this isn't how I'm supposed to create the object
      private boolean published;
      private SimpleDateFormat a = new SimpleDateFormat("yyyy-MM-dd 'at' hh:mm:ss");
     
     
      public Article(String title) {
        this.title = title;
      }
     
      public Article(String title, String body, ArrayList<Author> author, GregorianCalendar pubDate,
                       boolean published) {
        this.title = title;
        this.body = body;
        this.authors = author; //somehow I'm supposed to pass the author class to this and create an arraylist
        this.pubDate = pubDate;
       // this.pubDate.set(year, month - 1, dayOfMonth, hour, minute);
        this.published = published;
      }

    My teacher says I shouldn't instantiate in the private fields (*or this is what I think he meant). If I create a object inside the constructor, where will the field be stored? wouldn't the object disappear? Do I have to create the object in the private fields area first?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Creating an object through a class parameters

    There are two steps:
    1) define a variable that will hold a reference to an instance
    2) assign a value to that variable.

    Those two steps can be done in different places.

    If a variable is defined inside a constructor or method, it will disappear when the constructor or method exits.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2014
    Location
    Boston, Ma
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Creating an object through a class parameters

    thanks, turns out he just didn't want me to create the new object in the private fields, but just create:

    private ArrayList <Author> authors;
    which (*I'm told) will be filled with data passed into the constructor.

Similar Threads

  1. Is there an easier way for creating shape parameters?
    By jRele in forum AWT / Java Swing
    Replies: 0
    Last Post: September 23rd, 2013, 03:38 AM
  2. Replies: 2
    Last Post: July 19th, 2013, 07:27 PM
  3. Creating object everytime object is called
    By aandcmedia in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 12th, 2012, 04:18 PM
  4. Creating and implementing class for creating a calendar object
    By kumalh in forum Object Oriented Programming
    Replies: 3
    Last Post: July 29th, 2011, 08:40 AM
  5. Replies: 7
    Last Post: July 21st, 2011, 02:29 PM

Tags for this Thread