x404.co.uk
http://www.x404.co.uk/forum/

Learning Java
http://www.x404.co.uk/forum/viewtopic.php?f=4&t=5838
Page 1 of 1

Author:  KRKux [ Fri Jan 29, 2010 5:14 pm ]
Post subject:  Learning Java

hey guys,

Couldn't find a sub forum for programming and has been long time since I have been on here, so shall just post here! Been busy in Uni with computer science course in Newcastle!

Anyway what I need help with is understanding this little bit in Java.

I have a book that says the construction of a method should always be like this;

public static void main(String[] args)

And that main is that starting point of all java applications and cannot be changed

However in lectures we made the amazing Hello, World! application but my lecturer constructed the class like this;

public static void HelloWorld()

So why does the book say use main for the program and my lecturer used HelloWorld?

Probably really simple but its a long day :)

Author:  Alexgadgetman [ Fri Jan 29, 2010 5:32 pm ]
Post subject:  Re: Learning Java

Have you asked her yet? ;) (couldnt resist :D )

Author:  KRKux [ Fri Jan 29, 2010 5:34 pm ]
Post subject:  Re: Learning Java

:lol:
















No.




I don't see her anymore, plenty of new girls here which I talk to :).

Author:  Fogmeister [ Fri Jan 29, 2010 5:40 pm ]
Post subject:  Re: Learning Java

Oh, ignore my previous reply.

You are interpreting your text book incorrectly.

When you create a method you do it in the form...

public void myMethod()
{

}

there are variations like making the method private instead of public or making it return an int, string, etc... instead of void.

HOWEVER!

In order to know where to start, your program needs...

public static void main(String [] args)
{

}

Without this your program won't do anything as it won't know where to run.

Author:  Nick [ Fri Jan 29, 2010 10:04 pm ]
Post subject:  Re: Learning Java

KRKux wrote:
I have a book that says the construction of a method should always be like this;

public static void main(String[] args)


main() is a special method - it is the starting point for the application.

Quote:
And that main is that starting point of all java applications


Exactly.

Quote:
However in lectures we made the amazing Hello, World! application but my lecturer constructed the class like this;

public static void HelloWorld()


The above is a method, which would have formed part of a class.

I am going to bet that your lecturer had something similar to the following code:

Code:
public class DemoApp {         
public static void main(String args[])
{
   System.out.print("Hello");
   DemoApp.HelloWorld();
}
public static void HelloWorld() {
   System.out.print(" world!");
}
}


Here, you can see two methods - main and HelloWorld. The main() is where the program begins, and HelloWorld() is called from within main().

Author:  KRKux [ Sun Jan 31, 2010 12:19 am ]
Post subject:  Re: Learning Java

Hey all, sorry for late reply have been busy!

Heres the code he used to clear up the confusion!

Code:
public class HelloWorld
{
   public static void printMessage()
   {
      System.out.println("Hello World");
   }
}


:)

Author:  forquare1 [ Sun Jan 31, 2010 12:51 am ]
Post subject:  Re: Learning Java

KRKux wrote:
Hey all, sorry for late reply have been busy!

Heres the code he used to clear up the confusion!

Code:
public class HelloWorld
{
   public static void printMessage()
   {
      System.out.println("Hello World");
   }
}


:)


Try it out, it doesn't work:
Code:
faegilath:Desktop benlavery$ javac HelloWorld.java
faegilath:Desktop benlavery$ java HelloWorld.
faegilath:Desktop benlavery$ java HelloWorld
Exception in thread "main" java.lang.NoSuchMethodError: main


Not every class needs a main, only the "main" class. Did he not do something like:
Code:
public class Main
{
   public static void main()
   {
      HelloWorld.printMessage();
   }
}


or

Code:
public class HelloWorld
{
   public static void printMessage()
   {
      System.out.println("Hello World");
   }

   public static void main(String[] args)
   {
        HelloWorld.printMessage();
   }

}

?

An interesting point may be (and I've no idea why he'd be telling you this unless you were looking at ultra-advanced stuff) that you can somehow change the name of the main method...No idea, probably some JVM jiggery pockery, or some fancy setting somewhere.

I'd ask your lecturer for clarification, because as far as I know, your program will always start in a class with a main method.

Author:  finlay666 [ Sun Jan 31, 2010 2:11 am ]
Post subject:  Re: Learning Java

KRKux wrote:
Been busy in Uni with computer science course in Newcastle!



I would say I'd hunt you down for a drink.... but I really can't be arsed and the bar I would have gone to would have been full of girls, however judging from the OLD thread you would be like Raj from BBT...sober.

:lol:

is it Newcastle or Northumbria? Can suggest some books in the library you should have a look at, and even some lecturers to talk to depending on uni

Author:  KRKux [ Sun Jan 31, 2010 12:55 pm ]
Post subject:  Re: Learning Java

Forquare it work for me when I run it, I use BlueJ if that matters...
I shall ask him next time I see him.


And Finlay I go to Newcastle....not the poly!

Author:  forquare1 [ Sun Jan 31, 2010 1:20 pm ]
Post subject:  Re: Learning Java

I would presume BlueJ is doing something to make it run then...

Author:  Nick [ Sun Jan 31, 2010 2:24 pm ]
Post subject:  Re: Learning Java

Ditch BlueJ - it's a piece of crap.

Do it properly!!

Author:  finlay666 [ Sun Jan 31, 2010 3:21 pm ]
Post subject:  Re: Learning Java

KRKux wrote:
And Finlay I go to Newcastle....not the poly!


You got the worse uni then for computer science courses :P

Author:  EddArmitage [ Sun Jan 31, 2010 6:24 pm ]
Post subject:  Re: Learning Java

Nick wrote:
Ditch BlueJ - it's a piece of crap.

Do it properly!!

++;

Having said that, we had a fortnight of lectures using it. Don't think they do anymore though, based on our feedback.

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/