Results 1 to 3 of 3
Like Tree1Likes
  • 1 Post By jim829

Thread: Trying to call a method/constructor from the main class????

  1. #1
    dazz is offline Member
    Join Date
    Apr 2017
    Posts
    27
    Rep Power
    0

    Default Trying to call a method/constructor from the main class????

    Hello

    This should be a very basic question. I have created a class below with one method.

    Java Code:
    /*
    package flag;
    import flag.panel.FlagDescriptor;
    import java.util.ArrayList;
    import java.awt.Color;
    import org.apache.logging.log4j.LogManager;
    import org.apache.logging.log4j.Logger;
    
    public class  LoadAllFlags {
    
        private final static Logger log = LogManager.getLogger(LoadAllFlags.class); 
        private int i=0;  // counter
        private final int iMax = 30;// max number of flags in the array
    
        private  ArrayList<FlagDescriptor> flagList;
        //the array containing a list of all flags
        // public  ArrayList<FlagDescriptor> flagList = new ArrayList<FlagDescriptor>(iMax); //initial capacity of 30 objects
        // to iterate through the list : for (Gun i: flagList){ System.out.println(i)}
     
        public void setAllFlags(){    //load blank flag.  Always ID= 0
            flagList = new ArrayList<FlagDescriptor>(iMax);
            FlagDescriptor blankFlag = new FlagDescriptor.FlagDescriptorBuilder(
                                        0,
                                        "Blank",
                                        "").build();
            flagList.add(blankFlag);
        }  
    }
    I am trying to call the above from the main class below.
    I tried to make setAllFlags static, but I can't because it contains dynamic variables (ArrayList)
    I tried making it a constructor by calling the method LoadAllFlags.

    The import statement does not cause an error message.
    The "setAllFlags();" has the error" can't find symbol method setAllFlags.
    So for some reason, Netbeans IDE can't find the method/constructor.
    The solution is probably very simple, but I don't know what I am doing wrong.

    Java Code:
    package flag;
    import flag.LoadAllFlags;
    
    public class FlagMain{
        
        private final static Logger log = LogManager.getLogger(FlagMain.class);
    
        public static void main(String[] args) {     
    
          log.info("starting...");  
          setAllFlags();
        
        }
    }

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,193
    Rep Power
    12

    Default Re: Trying to call a method/constructor from the main class????

    You cannot call an instance method or field from a static context unless you qualify it with an instance of the containing
    class.

    So do it like this:

    Java Code:
    LoadAllFlags laf = new LoadAllFlags();
    laf.setAllFlags();
    If you make setAllFlags a static method, you could do it. But then you can't access the instance field(s) from that
    method because, once again, you can't access instance fields from static contexts without qualifying them. In this
    case you could not access the instance field, flagList.

    In general, only use static methods which operate on a passed value and return some result. Check out the
    Math class for examples.

    Edit: I forgot to mention that constructors are invoked with the new operator (or using some reflective methods).
    But you don't really call a constructor.

    Regards,
    Jim
    Last edited by jim829; 05-13-2017 at 05:21 PM.
    jepoys1 likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    dazz is offline Member
    Join Date
    Apr 2017
    Posts
    27
    Rep Power
    0

    Default Re: Trying to call a method/constructor from the main class????

    Hi Jim

    Your answer is spot on.
    I am still on the lower left corner of the learning curve and still climbing.

    Thanks.
    Dazz

Similar Threads

  1. How to call a constructor in main
    By ymbulk in forum New To Java
    Replies: 4
    Last Post: 07-03-2014, 05:58 PM
  2. How to call a class into the main method 5 times?
    By Valerie10b in forum New To Java
    Replies: 4
    Last Post: 03-16-2013, 09:02 AM
  3. Overridable method call in the constructor - question
    By mcdhappy80 in forum New To Java
    Replies: 2
    Last Post: 01-24-2013, 09:58 AM
  4. Unable to call classe constructor from main
    By serdimay in forum New To Java
    Replies: 13
    Last Post: 08-30-2011, 10:38 AM
  5. No main method, constructor/object not working
    By sinista in forum New To Java
    Replies: 9
    Last Post: 10-14-2009, 02:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •