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
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Java allows calling of method name == class name from main but nothing else.

  • 27-04-2013 6:42pm
    #1
    Registered Users, Registered Users 2 Posts: 387 ✭✭


    Hi.

    Can anyone explain the following:

    I have an example class Test and a three public methods including main.
    1. Main
    2. Test
    3. Hello

    Q1. Why are you allowed declare a Method with the same name as the Class without a return type.
    Q2: Why are you allows call that method from Main but any other method gives an error.

    Am I actually call a new instance of the class and the default behavour is to call execute the method with the same name. This is the only thing that makes sense to me.
    public class Test
    {
      public static void main(String[] args)
       { 
         new Test();
         new Hello();
       }
     
    
      public Test()
        {
          System.out.println("Method ServerMain");
        }
     
    
      public void Hello()
        {
          System.out.println("Method Hello");
        }
    
    }
    


Comments

  • Registered Users, Registered Users 2 Posts: 1,352 ✭✭✭Phibsboro


    The method with no return and the same name is a special method called a constructor - it is called automatically when an instance of this class is created.

    When you do new Test(), you aren't calling that method, you can creating a new instance of the Test class, which in turn calls the constructor.

    The syntax for calling a method doesn't use "new", you just use the method name with any required arguments, e.g. Hello();


  • Registered Users, Registered Users 2 Posts: 387 ✭✭peter_dublin


    Phibsboro wrote: »
    The method with no return and the same name is a special method called a constructor - it is called automatically when an instance of this class is created.

    When you do new Test(), you aren't calling that method, you can creating a new instance of the Test class, which in turn calls the constructor.

    The syntax for calling a method doesn't use "new", you just use the method name with any required arguments, e.g. Hello();

    Thanks Phibsboro;

    Makes perfect sense now. So when Main is called you use to create an instance of your class. Completely missed that and Google was useless trying to find info. Worse still I just read that last night in "Learning Java" but missed it when it came to writing actual code.

    Cheers
    Peter


  • Closed Accounts Posts: 2,113 ✭✭✭SilverScreen


    Peter you need to read up a bit more on classes, objects, constructors and methods.

    By calling 'new Test()' you are creating a new Test instance and calling its constructor. In order to call the Hello() method you can use 'new Test().Hello()' inside the main method.


  • Registered Users, Registered Users 2 Posts: 387 ✭✭peter_dublin


    Zero1986 wrote: »
    Peter you need to read up a bit more on classes, objects, constructors and methods.

    By calling 'new Test()' you are creating a new Test instance and calling its constructor. In order to call the Hello() method you can use 'new Test().Hello()' inside the main method.

    Thanks Zero1986.

    Yes I knew about instanciating objects and methods etc but I completly missed the part about instanciating the class itself and how give the fact those classes have no main method would code execution start. Seems In my hast I missed some fundemental details.

    Thanks


  • Registered Users, Registered Users 2 Posts: 1,352 ✭✭✭Phibsboro


    Thanks Zero1986.

    Yes I knew about instanciating objects and methods etc but I completly missed the part about instanciating the class itself and how give the fact those classes have no main method would code execution start. Seems In my hast I missed some fundemental details.

    Thanks

    It isn't really the case that the constructor is how code execution starts - thats a procedural way of thinking :D An instance of a class can execute code by having one of its methods called. The constructor is just a once off bit of code that is called when the instance is set up - it can be used for example to copy in arguments to help set up the instance.

    A class would't normally create an instance of itself (thats an advanced use). To understand it better you should always mess around with at least two classes, one with the main in it that just starts off the code and instaniates the other class(es).

    Are you using a book to learn this? It sounds like you are coming from a procedural coding background and the move to Object Orientated can be very confusing initially. I'd definitely recommend a book!


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 387 ✭✭peter_dublin


    Phibsboro wrote: »
    It isn't really the case that the constructor is how code execution starts - thats a procedural way of thinking :D An instance of a class can execute code by having one of its methods called. The constructor is just a once off bit of code that is called when the instance is set up - it can be used for example to copy in arguments to help set up the instance.

    A class would't normally create an instance of itself (thats an advanced use). To understand it better you should always mess around with at least two classes, one with the main in it that just starts off the code and instaniates the other class(es).

    Are you using a book to learn this? It sounds like you are coming from a procedural coding background and the move to Object Orientated can be very confusing initially. I'd definitely recommend a book!

    Hi.

    Yeah, I mainly programme in C with some VB6 a long time ago :-) and some ASP.NET when it was launched and we were speaking about HTML4 and this "new" standard called XML which would change the world at the turn of the millenium :-)

    Since then I moved into ERP and Networking to a high level but I believe all IT Professionals should be able to code proficiently in at least one low and high level language so decided to throw a hand at Java as I have several complex command line only tools I plan to write a Global UI for which we use for performing distributed Unicode Conversions of complex hetrogenerous ERP Systems which uses several nice features of Java such as Netowrking, Multithreading and communication using XML messages.

    I started with Head First Java 2nd Edition and while it's very good in some aspects, I don't feel it is detailed enough so I am now reading Learning Java 3rd edition.

    Thanks


  • Closed Accounts Posts: 7,967 ✭✭✭Synode


    distributed Unicode Conversions of complex hetrogenerous ERP Systems

    Myself and my wife were just discussing these last night *


    * may not be true :pac:


Advertisement