• Post Reply Bookmark Topic Watch Topic
  • New Topic

Single Main class into Multiple class.  RSS feed

 
Miko Jing
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi i'm newbie in java program.. Normally or every time doing java code, i will always doing it in one java main class.. And now i was wondering how to create a mutiple class from one main class? By calling method? abit confuse.. here's my main java file

 
Ariel Krakowski
Greenhorn
Posts: 5
Chrome Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see that code is from http://www.newthinktank.com/2012/07/java-video-tutorial-49/.

Anyways, you need to split up the functionality of your program into different classes. Your code can then create instances of other classes. For example, you would separate the graphical part of a a program from another part, such as the logical part. In your code, many different instances are created of various Java Classes, such as JButton or JSlider. You can read more about Object-oriented programming on my tutorial here: http://www.learneroo.com/modules/18.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You alos should split those methods. The mouseReleased method is far too long, and all those ifs are potential sources of error.
Don't use == 2 etc. Create a constant which represents 2 and give it a simple name.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Miko Jing wrote:Normally or every time doing java code, i will always doing it in one java main class.

Bad move...as you appear to be discovering.

And now i was wondering how to create a mutiple class from one main class? By calling method? abit confuse...

You may find the MainIsAPain page worth reading, because only ONE of your classes needs a main() method - and it should be tiny.

To start out with, you're probably best off to keep all related classes in a single package, and then you create and use them just like you would any other Java class, eg:
String msg = new String("Well, Hello World");
System.out.println( msg.subtring(6) );
(The underlined names are Java classes, and the new is for illustration only)

Later on, you'll learn how to arrange them more "smartly" and use the import statement.

But, as most of the others have said, some of your methods are much too big. My rule of thumb is a screenful of code, MAX (≈30-40 lines), but you might want to start out even smaller.

So, a couple of basic rules that may help you:

1. Before you write ONE LINE of Java code, make sure you know what you want to do. Write it out on paper, in detail, and in English (or your native language) first.
For more info on this, you may find the WhatNotHow (←click→) and StopCoding pages useful.

2. If you have a method that is more than ≈20 lines long, break it up. You won't always be able to do this, but alarm bells should start to go off when you find yourself writing enormous methods.

HIH

Winston
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!