Reply to topic  [ 13 posts ] 
Learning Java 
Author Message
Occasionally has a life
User avatar

Joined: Thu Apr 23, 2009 6:50 pm
Posts: 278
Location: London / Bedfordshire / Newcastle
Reply with quote
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 :)

_________________
I had the last spam thread in the TMP. :)


Fri Jan 29, 2010 5:14 pm
Profile
Occasionally has a life

Joined: Fri Apr 24, 2009 4:56 pm
Posts: 306
Reply with quote
Have you asked her yet? ;) (couldnt resist :D )


Fri Jan 29, 2010 5:32 pm
Profile
Occasionally has a life
User avatar

Joined: Thu Apr 23, 2009 6:50 pm
Posts: 278
Location: London / Bedfordshire / Newcastle
Reply with quote
:lol:
















No.




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

_________________
I had the last spam thread in the TMP. :)


Fri Jan 29, 2010 5:34 pm
Profile
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 7:35 pm
Posts: 6580
Location: Getting there
Reply with quote
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.

_________________
Oliver Foggin - iPhone Dev

JJW009 wrote:
The count will go up until they stop counting. That's the way counting works.


Doodle Sub!
Game Of Life

Image Image


Fri Jan 29, 2010 5:40 pm
Profile WWW
Spends far too much time on here
User avatar

Joined: Thu Apr 23, 2009 11:36 pm
Posts: 3527
Location: Portsmouth
Reply with quote
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().

_________________
Image


Fri Jan 29, 2010 10:04 pm
Profile
Occasionally has a life
User avatar

Joined: Thu Apr 23, 2009 6:50 pm
Posts: 278
Location: London / Bedfordshire / Newcastle
Reply with quote
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");
   }
}


:)

_________________
I had the last spam thread in the TMP. :)


Sun Jan 31, 2010 12:19 am
Profile
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 6:36 pm
Posts: 5150
Location: /dev/tty0
Reply with quote
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.


Sun Jan 31, 2010 12:51 am
Profile WWW
Spends far too much time on here
User avatar

Joined: Thu Apr 23, 2009 9:40 pm
Posts: 4876
Location: Newcastle
Reply with quote
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

_________________
Twitter
Charlie Brooker:
Macs are glorified Fisher-Price activity centres for adults; computers for scaredy cats too nervous to learn how proper computers work; computers for people who earnestly believe in feng shui.


Sun Jan 31, 2010 2:11 am
Profile
Occasionally has a life
User avatar

Joined: Thu Apr 23, 2009 6:50 pm
Posts: 278
Location: London / Bedfordshire / Newcastle
Reply with quote
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!

_________________
I had the last spam thread in the TMP. :)


Sun Jan 31, 2010 12:55 pm
Profile
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 6:36 pm
Posts: 5150
Location: /dev/tty0
Reply with quote
I would presume BlueJ is doing something to make it run then...


Sun Jan 31, 2010 1:20 pm
Profile WWW
Spends far too much time on here
User avatar

Joined: Thu Apr 23, 2009 11:36 pm
Posts: 3527
Location: Portsmouth
Reply with quote
Ditch BlueJ - it's a piece of crap.

Do it properly!!

_________________
Image


Sun Jan 31, 2010 2:24 pm
Profile
Spends far too much time on here
User avatar

Joined: Thu Apr 23, 2009 9:40 pm
Posts: 4876
Location: Newcastle
Reply with quote
KRKux wrote:
And Finlay I go to Newcastle....not the poly!


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

_________________
Twitter
Charlie Brooker:
Macs are glorified Fisher-Price activity centres for adults; computers for scaredy cats too nervous to learn how proper computers work; computers for people who earnestly believe in feng shui.


Sun Jan 31, 2010 3:21 pm
Profile
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 9:40 pm
Posts: 5288
Location: ln -s /London ~
Reply with quote
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.

_________________
timark_uk wrote:
Gay sex is better than no sex

timark_uk wrote:
Edward Armitage is Awesome. Yes, that's right. Awesome with a A.


Sun Jan 31, 2010 6:24 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 13 posts ] 

Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group
Designed by ST Software.