[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Basic java doubt

Posted on 2012-03-21
7
Medium Priority
?
535 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 26

Assisted Solution

by:ksivananth
ksivananth earned 600 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 400 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 400 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

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.
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month20 days, 13 hours left to enroll

865 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