Results 1 to 3 of 3
  1. #1
    hendrix79 is offline Member
    Join Date
    Dec 2008
    Posts
    28
    Rep Power
    0

    Default How to divide code in classes?

    Hello everyone!
    I'm new to java but i have some experience in programming. I just have a really newbie question: how do i divide java code in different classes?
    Let me explain it with a banal example.
    I need a program that has a button and a label. When i click on the button, i the label to change its content.
    This is my pseudo-code:


    graphics.java:
    class graphics {

    private JLabel Content;
    private JButton jLabelPassword;

    }

    actions.java:
    class actions {

    handler of what button does;

    }

    main.java:

    main class {

    I DON'T KNOW WHAT

    }

    ---end---

    so... can anybody please tell me how can i split code that i need in different classes. And... once i have actions and graphics, how do i start the program? What do i have to put in the main method?

    Thank you and sorry for the "stupid" question. :p

  2. #2
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Posts
    286
    Rep Power
    7

    Default What do you see as a "concept"?

    Organisation of your program into classes is largely arbitrary, and it takes a bit of practice to get a "natural feel" for where it seems right to make the split.

    To start with, try and use a separate class file for things that you see as a "concept":
    - things that you can "visualise" as a separate object (e.g. a window)
    - things that are re-used in different parts of your app

    Use inline classes or methods to additional classes for things that you see as just "uninteresting extensions or attributes" of more global/tangible objects. So for button listeners with only a few lines of code that are specific to a particular button or buttons in a particular window, just inline them there and then:

    Java Code:
    JButton b = ...
    b.addActionListener(new ActionListener() {
      public void actionPerformed(...) {}
    });

  3. #3
    hendrix79 is offline Member
    Join Date
    Dec 2008
    Posts
    28
    Rep Power
    0

Similar Threads

  1. Help with classes
    By gnarly hogie in forum New To Java
    Replies: 14
    Last Post: 10-10-2008, 03:29 PM
  2. Get name of available classes
    By escuja in forum CLDC and MIDP
    Replies: 0
    Last Post: 07-26-2008, 01:03 PM
  3. [SOLVED] Using Code Throughout 5 Classes-Stuck
    By Bascotie in forum New To Java
    Replies: 46
    Last Post: 06-06-2008, 06:51 AM
  4. When do we use inner classes?
    By cruxblack in forum New To Java
    Replies: 5
    Last Post: 08-10-2007, 06:00 PM
  5. Replies: 1
    Last Post: 04-26-2007, 04:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •