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

Some Java help for a noob

Options
  • 21-04-2005 4:59pm
    #1
    Closed Accounts Posts: 1,829 ✭✭✭


    Guys, I reading through Bruce Eckel's Thinking in Java 3 and I'm having some noobie issues with understanding. Have a look at the following simple example

    package c07.music;
    import com.bruceeckel.simpletest.*;

    public class Music {
    private static Test monitor = new Test();
    public static void tune(Instrument i) {
    // ...
    i.play(Note.MIDDLE_C);
    }
    public static void main(String[] args) {
    Wind flute = new Wind();
    tune(flute); // Upcasting
    monitor.expect(new String[] {
    "Wind.play() Middle C"
    });
    }
    } ///:~

    If I now create an object of type Music, is it fair to say that the main function is not accessible from this new object?
    Or would I get an error if I tried to create an object of type Music?

    Thanks
    JC

    Can I say that thhe


Comments

  • Closed Accounts Posts: 167 ✭✭Macmorris


    JackieChan wrote:
    If I now create an object of type Music, is it fair to say that the main function is not accessible from this new object?
    Or would I get an error if I tried to create an object of type Music?

    Just write a new function in the Music class that will execute the code you now have in the main function.


  • Closed Accounts Posts: 1,829 ✭✭✭JackieChan


    If I saved my class as is and created a new class and within this new class made an object of type Music(after importing it) would the main() in Music cause an error,be ignored or something else in the new object?


  • Registered Users Posts: 4,287 ✭✭✭NotMe


    Music is your main class because it has 'public static void main' in it. You can't create a Music object in another class.


  • Registered Users Posts: 347 ✭✭Static


    JackieChan wrote:
    If I saved my class as is and created a new class and within this new class made an object of type Music(after importing it) would the main() in Music cause an error,be ignored or something else in the new object?

    I'm not quite sure what you mean. The main() method is just a method like any other, the only thing special about it is that it's the 'start point' if you call the class from the command line, eg,

    java c07.music.Music

    Assuming of course, your classpath is set right :) You _could_ if you wanted to, have another class which can access main() in Music statically (since main() is static), so you could call Music.main(new String[0]);

    This is bad practice. A main() method should simply exist to prepare and call any routines to start your application, should it be called from the command line. If the main in your sample app contains code that you would like other classes to be able to use, consider breaking that code out into another method. I haven't compiled this, but here's an idea.
    public class MathUtils
    {
       public static void main(String[] args)
       {
          // add two numbers, display result
          // subtract two numbers, display result
       }
    }
    

    That's all great, but you've tied all your stuff into your main, making for poor modularisation and reusability. Perhaps consider the following...
    public class MathUtils
    {
       public int add(int a,int b)
       {
          return a + b;
       }
     
       public int subtract(int a,int b)
       {
          return a - b;
       }
    
       public static void main(String[] args)
       {
           MathUtils utils = new MathUtils();
           // display result of utils.add(1,2)
           // display result of utils.subtract(10,9)  
       }
    }
    
    
    public class SomeOtherClass
    {
       public static void main(String[] args)
       {
           MathUtils utils = new MathUtils(); 
           // I can use utils.add() and utils.subtract() for
           // my own purposes here  
       }
    }
    

    I hope I understood what your question was, and didn't answer something completely unrelated ;)


  • Closed Accounts Posts: 1,829 ✭✭✭JackieChan


    Static,
    You have answered my questions
    Thanks
    JC
    PS:I will be back with more!


  • Advertisement
Advertisement