Solved

Basic java doubt

Posted on 2012-03-21
7
522 Views
Last Modified: 2012-06-27
Class1

Test1 name_instance = new Test1("Abc");
Test1 empty_const = new Test1();

Open in new window




Class2
String name;//global

public Test1(String name){
	this.name = name;
	System.out.println("Name is "+name);
}

public Test1(){
	System.out.println("Name in empty constructor "+name);
}

Open in new window




Output
Name is Abc
Name in empty constructor null

Open in new window



Is there a way out by which I can persist the name value in empty constructor, such that the name is printed as Abc in empty constructor also? Any help in this regard is much appreciated.
0
Comment
Question by:Sreejith22
7 Comments
 
LVL 26

Expert Comment

by:ksivananth
ID: 37745966
set default value to 'name' while decalring itself, e.g.

String name = "ABC" ;
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37745973
Why do you need it?

You can create with empty constructore and then have method setName(String) to set the name and method ti print the name
0
 

Author Comment

by:Sreejith22
ID: 37745974
I have presented my problem in a simpler manner.

I somehow want to prevent the object becoming null while the empty constructor is called each time. I do not want to initialise the variable, but need to retain the variable if it is already initialized and assigned a value
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 26

Assisted Solution

by:ksivananth
ksivananth earned 150 total points
ID: 37745978
>>retain the variable if it is already initialized and assigned a value

make it static!
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37745980
But you initialized another instance of that class
Just don;t prrovide empty constructor and it will never be null
0
 
LVL 47

Assisted Solution

by:for_yan
for_yan earned 100 total points
ID: 37745988
>to retain the variable if it is already initialized

If you name is instance variable then in each insatnce it will be separate form another - if you intialized it in one instance - does not say anything about its value in another instance
0
 
LVL 10

Accepted Solution

by:
gordon_vt02 earned 100 total points
ID: 37754878
Sounds like you might want to use a singleton pattern:

public class Test1 {
    private static Test1 instance;

    public static Test1 initialize(String name) {
        if (instance != null && !instance.getName().equals(name))
            throw new IllegalStateException("Test1 is already initialized.");
        
        if (instance == null)
            instance = new Test1(name);
        
        return instance;
    }

    public static Test1 getInstance() {
        return instance; // null if uninitialized
    }

    private final String name;

    // private constructor -- only Test1 may instantiate itself
    private Test1(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }
}

Open in new window

0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
The viewer will learn how to implement Singleton Design Pattern in Java.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now