• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Single Main class into Multiple class.

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Greenhorn
Posts: 5
Mac OS X Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Your mother is a hamster and your father smells of tiny ads!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic