Jump to content

why does this work, why would you do it.

- - - - -

  • Please log in to reply
6 replies to this topic

#1
zeroradius

zeroradius

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,406 posts
So I'm going thou my head first java book and i see this:

public class MiniMiniMusicApp
{
   public static void main(String[] args)
   {
       MiniMiniMusicApp mini = new MiniMiniMusicApp();
.
.
.
.random stuff here
.
.
}
.
.
. other classes
.
.
}
 


so its a method calling the class that it belongs to????????? I don't understand. Why is this done?
Posted Image

#2
Sinipull

Sinipull

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 386 posts
Yes, the main method creates an instance of a class it belongs to. I don't know why this is considered good approach, but it eliminates the need for static access. One (very experienced) of my Java teachers used to do it too.

#3
zeroradius

zeroradius

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,406 posts
thanks for the reply.

wouln't it be better for resources to just use static thou, one less thing on the heep (did i get that right, ive been mixing up heep and stack) wich means less memory right?
Posted Image

#4
Sinipull

Sinipull

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 386 posts
it's "heap". But i don't think it has anything to do with saving memory (It's a very little difference, if at all). Static class must also be somewhere in the memory.

#5
zeroradius

zeroradius

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,406 posts
Ah I thought you meant static method sense it would be in the same class. (note I'm rather new to app programming, only ever worked with web technologies and I was never good at that either)
Posted Image

#6
Momerath

Momerath

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 243 posts
Some people do this because 'static' means it's globally available and global = bad.

I do it when I'm too lazy to make another class and I'm going to need multiple instances (usually testing an idea. Production code gets its own class)

#7
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Well, it has to be static because it's the only kind of stuff you can access before creating an actual instances of a class.
( if your class has a "public String toString()" method, you won't be able to reach that method until you got an object/instace of the class of which you can call the toString()
Static stuff can be called without needing an instance )

Imo, a good main method (where good means that your whole program isn't programmed in the main) usually has only 1-2 lines: create an instance from a class. And then call a start() method or something like that of that instance.
Note that the main method can use any class you want. It doesn't have to create the class that's encapsulating it.

I usually create 1 'Test' class, next to the others of my project, which contains the main method. But i never create a 'Test' instance.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users