Solved

Basic java doubt

Posted on 2012-03-21
7
523 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
@SBGen Method 3 36
Java / Linux and Regular Expressions 11 69
session migration servlets 2 25
Windows 10 and JDK 7 7u85 4 31
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

911 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

20 Experts available now in Live!

Get 1:1 Help Now