Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Issue with setting up a program architecture

Options
  • 18-01-2017 9:08pm
    #1
    Registered Users Posts: 11


    Hi,

    I'm starting to learn some programming and am struggling on something really basic. :(

    I've got the following 3 classes at the same level in Eclipse (i.e. all within the same package):
    package beginings;
    
    public class01
    {
       public void method01
       {
          stuff
       }
    }
    
    package beginings;
    
    public class02
    {
       public void method02
       {
          stuff
       }
    }
    

    In my 3rd class, I want to be able to call both the methods in each of the classes:
    package beginings;
    
    public class entry
    {
       public static void main(String args[])
       {
          beginings.class01.method01;
          beginings.class02.method02;
       }
    }
    

    What am I doing wrong here? I don't think I require an import as both called methods are within the same package.


    Thanks.


Comments

  • Registered Users Posts: 6,252 ✭✭✭Buford T Justice


    Your methods that you want to call must be declared static, otherwise you will need to create instances of each class (objects) before you can access their methods provided their not private.
    You've also missed some syntax issues, such as declaring the keyword class before each class definition, and the parentheses () after the method declaration.
    You've mixed up the declaration of the main method, the [] are in the wrong place
    package beginings;
    
    [B]class [/B]class01 {
        public static void method01[B]()[/B] {
        }
    }
    
    [B]class [/B]class02 {
        public static void method02[B]()[/B] {
        }
    }
    
    public class entry
    {
     public static void main(String[] args) 
       {
          beginings.class01.method01;
          beginings.class02.method02;
       }
    }
    


  • Registered Users Posts: 586 ✭✭✭Aswerty


    An alternative to using the static keyword is to create an instance of each class, then you can call the methods via the respective instances. Also, just note that when calling a method you should always use the curved brackets () after the method name (or if the method has an argument you pass the argument inside the curved brackets).

    So something along the lines of...
    package beginings;
    
    class class01
    {
        public void method01() 
        {
            // stuff
        }
    }
    
    class class02
    {
        public void method02()
        {
            // stuff
        }
    }
    
    public class entry
    {
       public static void main(String[] args) 
       {
          // we create an instance of each class
          class01 c1 = new class01();
          class02 c2 = new class02();
    
          // then we call the methods via the instance name and not via the class name
          c1.method01();
          c2.method02();
       }
    }
    


  • Registered Users Posts: 6,252 ✭✭✭Buford T Justice


    Best not to use objects until the foundations are understood first no?


  • Registered Users Posts: 586 ✭✭✭Aswerty


    Best not to use objects until the foundations are understood first no?

    With Java I would have assumed that using static (other than in the main method) would have been more exotic than using objects? No? You can't really progress too far without making object instances. I could be wrong, been a while since I've seen a novice curriculum. Either way hopefully one approach or the other will jog the OPs memory as to whatever material they've been learning from.


  • Registered Users Posts: 6,150 ✭✭✭Talisman


    Aswerty wrote: »
    With Java I would have assumed that using static (other than in the main method) would have been more exotic than using objects? No? You can't really progress too far without making object instances. I could be wrong, been a while since I've seen a novice curriculum. Either way hopefully one approach or the other will jog the OPs memory as to whatever material they've been learning from.
    There's two schools of teaching in academic institutions at the moment Early Objects and Late Objects, Deitel & Deitel have a book for both!

    Java How to Program, 10/e (Early Objects)

    Java How to Program, 10/e, Late Objects Version

    In my limited experience of tutoring some students last year:
    - The people that followed the Early Objects material grasp OO better but they couldn't write a program.
    - The Late Objects students could write code but struggled with OO.


  • Advertisement
  • Registered Users Posts: 6,252 ✭✭✭Buford T Justice


    Talisman wrote: »
    There's two schools of teaching in academic institutions at the moment Early Objects and Late Objects, Deitel & Deitel have a book for both!

    Java How to Program, 10/e (Early Objects)

    Java How to Program, 10/e, Late Objects Version

    In my limited experience of tutoring some students last year:
    - The people that followed the Early Objects material grasp OO better but they couldn't write a program.
    - The Late Objects students could write code but struggled with OO.
    Aswerty wrote: »
    With Java I would have assumed that using static (other than in the main method) would have been more exotic than using objects? No? You can't really progress too far without making object instances. I could be wrong, been a while since I've seen a novice curriculum. Either way hopefully one approach or the other will jog the OPs memory as to whatever material they've been learning from.

    A little from column a, a little from column b really. I was thinking more along the lines of getting comfortable with the syntax and so on before getting into the nitty gritty of objects is all.


  • Registered Users Posts: 7,501 ✭✭✭BrokenArrows


    I would ignore statics until later on.

    OOP should be explained and understood first. Otherwise as a new programmer developing simple applications you could easily get into the mindset that "Objects are just too complicated and why dont i just make everything Static, its just easier. "


Advertisement