0

Is it the right Way we initialise the the object?

I have a class Sample I want to initialise the Object in my Sample1 Class After intialisation I dont require to use the object of Sample Class.

In that Case Can I initialise that object as new Sample(); instead of Sample s = new Sample(); if I am not going to use s anywhere in my Sample1 Class

My question, is this a good practice to do that? will there be any after affects

4 Answers 4

1

I commonly encounter the dilemma you describe in the main method of my applications; I want to initialize the application class, but don't need a reference to it after that, for example:

public class MyApplication {

    public MyApplication() {
        // Initialize and run application
    }

    public static void main(String[] args) {
        new MyApplication();
    }
}

Some consider this a code smell. However adding a local variable or field just to keep a useless reference also seems wrong. The short answer to your question: there are no negative side effects and whether the design is bad or not is up for debate.

One way 'around' this is to be more puristic about your constructor. One could reason constructors should be used for initialization only, not for starting up your application. Then you could separate these concerns as follows:

public class MyApplication {

    public MyApplication() {
        // Initialize application
    }

    public void run() {
        // Run application
    }

    public static void main(String[] args) {
        MyApplication app = new MyApplication();
        app.run();
    }
}

You can defend this as a valid design decision, and it also solves your unreferenced instance problem at the same time!

Sign up to request clarification or add additional context in comments.

2 Comments

@Adrian I disagree with your comment about not having any negative effects of your first example is up for debate. In software engineering, getting it work is no the only thing. Writing code that is READABLE, maintainable, extensible is also as important. If you just initialize it, it's hard to tell if it's doing anything important. It's even worse if you have s = new Object(). One might delete it seeing you never use it again. Your second example explicitly clarifies that you are doing something in the other object, thus better design choice.
@Monir I agree totally with your reasoning. With 'no negative side effects' I meant what I think the OP also meant: performance related issues like impaired garbage collection.
1

If you dont need it why initialise it ?

1 Comment

I have defined the methods in the class, So other class will access those methods by instantiating the class, But this class alone I am creating a just an instance and accessing those methods
0

You can do that but unless you make Sample a singleton you will be initializing it every time you create an object for it. I am not sure if this is what you want as you have not specified what you are initializing. There might be a better alternate way. Specify what you are initialzing or what is your requirement so that people can provide a more specific answer.

Comments

0

Yeah, that's alright. If you do not have any state (instance variable), you can very well go with a static method that does what your constructor is doing. And you just do, SampleClass.myMethod()

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.