[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Basic java doubt

Posted on 2012-03-21
7
Medium Priority
?
534 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: 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!

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses

649 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