Solved

Basic java doubt

Posted on 2012-03-21
7
531 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
The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

 
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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

688 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