2

I am new to java and stackoverflow

I have

 class Assemble()
 class Start()
 class Ignite()
 class Move()
...... There are still 12 classes 

I want use methods inside these classes but

  • i should not create objects of them
  • i cannot use extends for all these also

i there any way possible? Please bare anything silly, i am not able to figure out. And this is my first question hear.

the finaly class is

 class run
 {
 public void run_simple()
 { 
// hear i should be able to access all methods of above class
 }
 }
asked Mar 15, 2017 at 10:47
7
  • 2
    If the classes have static methods then you can call them without creating an object. Commented Mar 15, 2017 at 10:50
  • 1
    "i should not create objects of them" why? Java is an object oriented programming language. So you are supposed to use objects. If you feel that objects aren't a usefull concept use some other programming language! Commented Mar 15, 2017 at 10:50
  • But that way i should use each and every method with the class name Commented Mar 15, 2017 at 10:54
  • 1
    You have to get some concepts clear: In your files you write classes. objects only exist in the computers memory at runtime. extention is that one class inherits behavior from another class and adds some more behavior or redefines the base classes behavior. So is your request to have more than one class in a single file? Commented Mar 15, 2017 at 11:03
  • 2
    Creating objects will be tedious? But tedious works is what computers are there for! You don't think about actually writing code that instantiates each and every object explicitly on it's own, like SomeClass someObject1; SomeClass someObject2;..., are you? Know what, you tell us what you actually want to program and we help you with that, instead of some strange meta context. Commented Mar 15, 2017 at 11:15

1 Answer 1

3

If you use an object oriented language (as java) the way it is meant, your whole program is about creating and using objects (as mentioned in many comments). There are some valid technical reasons not to create objects and to use static methods ("it's tedious" is not one of them). There are environments that forbid to use inheritance.

Please state these reasons, otherwise we have to assume that you don't understand some basic concepts of object oriented languages and that your "restrictions" must be ignored.

Most "restrictions" of object oriented programming are intended to help you structure your solution/program. If you see them as real restrictions, the structure of your program might very well be bad.

I'd like to give an example on how something like this might look "the OO way". This might not fully match your project, but should show you that creating objects must not be an issue programmer effort wise.

First we need an interface that defines what one of your actions (thats what I call your classes) looks like

interface Action {
 public void run();
}

The following classes define the concrete actions. Their constructors might take parameters configuring details on how to execute them. In the run()-method of each class, you program on what an action does when executed.

class Assemble implements Action {
 public void run() {...}
}
class Start implements Action {...} 
class Ignite implements Action {...} 
class Move implements Action {...}

The controller does the "run everything". That's basically your "overhead" for creating objects!

class Controller {
 /** Returns a list of the configured action objects. */
 public static List<Action> buildActions() {
 List<Action> actions = new LinkedList<Action>();
 actions.add(new Assemble(parameter)); // or whathever parameters you need
 actions.add(new Start(parameter1, parameter2));
 actions.add(new Ignite());
 actions.add(new Move());
 }
 /** Build the list of actions and run one after the other. */
 public static void main(String[] args) {
 List<Action> actions = buildActions();
 for (Action action: actions) {
 action.run();
 // here you could add logging, profiling etc. per Action.
 }
 }
}
answered Mar 15, 2017 at 11:52
Sign up to request clarification or add additional context in comments.

1 Comment

This way i have not tried. possibly this will solve my problem , thank you.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.