Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Why we declare the constructor private?

  Asked By: James    Date: Jul 31    Category: Java    Views: 3408
  

Why we declare the constructor private .But if we declare a constructor private
it stops the inheratence why?

Share: 

 

29 Answers Found

 
Answer #1    Answered By: Eloise Lawrence     Answered On: Jul 31

Constructor are declared private  only when u want to prevent any other
class to create an instance of that object. This is generally required
when u want to implement Singleton design pattern and ensure that only
one instance exists for a particular object in the JVM

 
Answer #2    Answered By: Doyle Gonzalez     Answered On: Jul 31

Wouldn't that be the same as declaring the class as abstract?


 
Answer #3    Answered By: Balbir Kaur     Answered On: Jul 31

when u declare  the class abstract u cant have any instance of the class.
However, through the singleton design pattern and the private
constructor u can have only one instance of the class.

 
Answer #4    Answered By: Rene Sullivan     Answered On: Jul 31

No, a class with a private constructor  is still a concrete class. This
ensures that only one object can ever be created.

Example:
class Test{
private static Test test = new Test();
private Test(){}
public static Test getTest(){
return test;
}

}

 
Answer #5    Answered By: Milton Robinson     Answered On: Jul 31

No, because internal methods of the class can still contruct an
instance:

public final class MySingleton{

private MySingleton(){

}

public static MySingleton getInstance(){
return mySingletonInstance;
}

private static MySingleton mySingletonInstance = new MySingleton();

}

 
Answer #6    Answered By: Vinit Online     Answered On: Jul 31

u can use either blob or call the binary stream from the reslt set- this
technique is used in the above example. which approach to follow ? depends on
vaious factors (driver,heap size etc).

 
Answer #7    Answered By: Jake Williams     Answered On: Jul 31

First a question. Are you sure the stuff is getting into "a" in the first
place? From what you've given here, you've never created an array for "a" to
point at. Somewhere there needs to be an "a = new byte [nn];" to create the
actual array.

Second, you can't increase the size of an array. (You may already know this,
it's not clear from what you've said.) You have to create a new array and dump
the old one.

Next

byte c[a.length + 1];

should be

byte c[] = new byte [a.length + 1];

Then

c = a;

undoes the good work. It dumps the new array you just created and puts a
reference to the same array that "a" is referencing into "c". I.e. "a" and "c"
are now pointing to the first array. The new array no longer has a reference
and will be discarded.

To copy an array into a different-sized area, you need to use a "for" loop or

System.arraycopy (a, 0, c, 0, a.length);

Now

c[a.length + 1] = b;

is wrong too. The indexes of "a" run from a[0] to a[length-1]. The next
position in "c" is c[a.length].

Finally, you probably want the extended array back in "a". Here you can use

a = c;

to achieve what you want.

So, a little test program

byte[] a = new byte[3];
a[0] = 1;
a[1] = 2;
a[2] = 3;
byte[] c = new byte[a.length+1];
System.arraycopy (a, 0, c, 0, a.length);
c[a.length] = 4;
a = c;
for (int i = 0; i < a.length; i++)
System.out.println (a[i]);


demonstrates that "a" now has four items in it.

 
Answer #8    Answered By: Muriel Dunn     Answered On: Jul 31

try the System.arraycopy(Object src, int srcPos, Object dest, int destPos, int
Lenght) function =)

 
Answer #9    Answered By: Trae Thompson     Answered On: Jul 31

Did you try putting
SimpleBean.class under web-inf/classes &

Myjar.jar under web-inf/lib

 
Answer #10    Answered By: Rochelle Elliott     Answered On: Jul 31

thanks a lot for the solution ,it really works,i did it the way u told me,but it
is running on the forte 4 ce IDE but as soon as i uploaded it on tomcat server
it is not recognizing all package what shd i do

 
Answer #11    Answered By: Silvia Chapman     Answered On: Jul 31

I did a search and found this -->
http://otn.oracle.com/products/jdev/index.html

I guess I learned something new here too.

 
Answer #12    Answered By: Ty Thompson     Answered On: Jul 31

This link "How to Write, Use, and Test a Singleton Class " shd be usefull to
you.
http://www.devx.com/tips/Tip/18190

 
Answer #13    Answered By: Grady Stewart     Answered On: Jul 31

javaworld also has a tutorial for writing a singleton class

 
Answer #14    Answered By: Brendan Smith     Answered On: Jul 31

Jsp's translate into servlets. Regarding easiness, jsp's are easy to
write. Regarding powerful I would say servlets.

 
Answer #15    Answered By: Faiza Mian     Answered On: Jul 31

It is likely that you have left some tags without its
matching pair to start/end it. There was some text (maybe
blank) in between these, which is getting displayed.

mostly, it happens if you remove some part by global
replacement. Suppose you had some <Font ...>x</FONT> pair
and you removed <Font ...> part by global replacement, thus
x</font> got left there, and all these x are getting displayed.

It is more likely that such a part is left in some <table>
lines, so that it does not fit and html engine is pushing it up.

Do share the info about causes when you resolve it.

 
Answer #16    Answered By: Felix Gray     Answered On: Jul 31

Please always supply a subject for your questions. Also, have a search through
Yahoo Groups for JavaScript groups, which will be more useful for JavaScript
questions.

You can take a string entered by the user and convert it into a date field with:

var userDate = new Date (userString);

Note that the "new" is needed.

This will not validate that the string is a valid date format, nor will it be
entirely predictable about whether it's mm/dd/yy or dd/mm/yy format. You'll
remove ambiguity if you can get the format into yyyy/mm/dd.

You need to do a validation of the string before creating a date field.

Many sites are moving away from date strings and going to popup date pickers of
some sort if the date is to be converted to a date variable.

This one javascript.internet.com/calendars/date-picker.html will give you
a good example. This one http://www.calendarxp.net/ looks better. This
http://www.merlyn.demon.co.uk/js-date6.htm is a tutorial that will probably
provide you with more than you want.

You can also use drop-down lists: http://uts.cc.utexas.edu/~esuez/cal5.html

For more stuff, do a Google search with: javascript date picker.

The current date (actually the current date and time) can be obtained by:

var timeNow = new Date ();

You can simply do comparisons against this.

 
Answer #17    Answered By: Sultana Tabassum     Answered On: Jul 31

Check out the Calendar class................

 
Answer #18    Answered By: Hollie Hughes     Answered On: Jul 31

A class is used in Java and it contains bytecode this is then intrepreted by
the JVM

A exe is a binary file it can directly interact with the hardware of the
system

 
Answer #19    Answered By: Jackson Williams     Answered On: Jul 31

when u compile a java program it will create the code which JVM(Java Virtual
Machine) can understand & execute.
.class files are in the form of Byte code.

but if u r compiling a .c program & .cpp program the compiler will create the
.obj & .exe files. in my belief .exe is in machine code which can executed by
the operating system itself only where it was compiled. you can't execute the
same .exe file in diff o/s.

for example u can't compile a program in dos/windows O/S and u can't run in
Linux/ Solaris platform. becoz the compiled code was O/S oriented.

but Java programs are compiled for the JVM not for OS, so if there is an
compatible JVM in any OS u can run the same .class file. (with certain
restrictions., like filesystem, filehandling, etc.,) JVM will take care of OS
calls.

.NET compilers also has new idea of CLR(Common Language Runtime), like JVM it
compiles more than one language and create code for the CLR, so u can compile
multiple languages and make a same code, for ex u can compile COBOL,C++,C#
programs for CLR & run it through .NET framework.

all these things are just my beliefs only, if anything is wrong plz let me
know...

 
Answer #20    Answered By: Ethan Evans     Answered On: Jul 31

Reply embedded....................................

 
Answer #21    Answered By: Komal Mohammad     Answered On: Jul 31

there was a small mistake "A class file is created by the compiler when you
compile your code. I believe it can only be run by the compiler" the under
lined sentence is the mistake. the class file is made by the compiler but
the JVM uses the class file to run the program

 
Answer #22    Answered By: Chau Tran     Answered On: Jul 31

these helped a lot in understanding it.

In this case,

- what is a jar file? (how different from obj, class or exe?)

- how is obj file different from class/ jar files?

- how to get exe file of java programs? I have seen only
java, class and jar files.

- As everybody does development on one machine, one os, is
it possible to bypass creation of class files, and directly
get the executable, so that it gets executed fast?

After the testing is over, one can send class to other
machines/ other o/s, but in that case too, one would send
.java source and regenerate class file there.

In that case, what is the purpose of that intermediate stage
having class files.

 
Answer #23    Answered By: Viheke Fischer     Answered On: Jul 31

JAR means Java ARchive, it used to pack all your compiled .class files,
image files, etc., just before distributing your application.Because you packed
all in one file than it is easy to manage the entire application. One more
advantage of JAR file is that it is in a compressed form so you can save the
disk space & reduce the network traffic because of the reduced file size. (But
if you use compress method while creating a JAR, it takes a bit time to extract
the JAR while start-up of ur app so it takes some more start-up time of ur app..
(if I am wrong plz let me know)) so if you are using an applet based / swing
based or any other java programs you can make as a single JAR file after
compiling & distribute.

Note: You can directly execute a JAR file by command line or by just double
clicking in the latest releases (j2sdk1.4) because while installing java itself
it registers JAR extension I believe. for that you have set a manifest file
about the main-class,author,. etc.,

for more info about JAR, plz see the sun's site
java.sun.com/.../

I don't think there is any relationship between JAR file & OBJ file.. if any
plz clarify me..

.EXE form is basically for only Windows/DOS platform, you can't directly
execute .EXE files in other platforms like Linux,Unix,Solaris. But Java Classes
are platform independent by nature you can execute the .class files anywhere
there is an JVM(Java Virtual Machine). That means you can compile a class file
in windows & still u can use the same class file in Linux / Solaris. this method
is called Write Once, Run Anywhere (WORA) But still their is some tools like
J2EXE are there in Net, you can search & find it, that will make .class files
into .exe form I belief.

I agree.Class are executing little bit slower compared to OS dependant .EXE
files because it's very close to Operating System.

 
Answer #24    Answered By: Jeanette Greene     Answered On: Jul 31

JAR means Java ARchive, it used to pack all your compiled .class files,
image files, etc., just before distributing your application.Because you packed
all in one file than it is easy to manage the entire application. One more
advantage of JAR file is that it is in a compressed form so you can save the
disk space & reduce the network traffic because of the reduced file size. (But
if you use compress method while creating a JAR, it takes a bit time to extract
the JAR while start-up of ur app so it takes some more start-up time of ur app..
(if I am wrong plz let me know)) so if you are using an applet based / swing
based or any other java programs you can make as a single JAR file after
compiling & distribute.

Note: You can directly execute a JAR file by command line or by just double
clicking in the latest releases (j2sdk1.4) because while installing java itself
it registers JAR extension I believe. for that you have set a manifest file
about the main-class,author,. etc.,

for more info about JAR, plz see the sun's site
java.sun.com/.../

I don't think there is any relationship between JAR file & OBJ file.. if any
plz clarify me..

.EXE form is basically for only Windows/DOS platform, you can't directly
execute .EXE files in other platforms like Linux,Unix,Solaris. But Java Classes
are platform independent by nature you can execute the .class files anywhere
there is an JVM(Java Virtual Machine). That means you can compile a class file
in windows & still u can use the same class file in Linux / Solaris. this method
is called Write Once, Run Anywhere (WORA) But still their is some tools like
J2EXE are there in Net, you can search & find it, that will make .class files
into .exe form I belief.

I agree.Class are executing little bit slower compared to OS dependant .EXE
files because it's very close to Operating System.

 
Answer #25    Answered By: Isabella Campbell     Answered On: Jul 31

Try sending this Javascript variable value as a form object. That will
solve your problem.

 
Answer #26    Answered By: Logan Bouchard     Answered On: Jul 31

I don't know what precisely you mean by "single cell". If you mean a cell in a
<table> or something similar, then the answer is certainly no. The database
knows nothing about your cells.

In general, fields in a database record only hold a single item. You are quite
at liberty to format multiple pieces of data into this single item, of course,
but to the database it's still a single item.

 
Answer #27    Answered By: Aidyn Smith     Answered On: Jul 31

looping in javascript is just like looping in Java. which loop makes u
desperate? your question should be more spesific. we can't understand what do u
mean

while (true condition){
your code here;
}

do {

your code here;

}while (true condition);

for (true condition){
your code here;
}

 
Answer #28    Answered By: Abbad Akhtar     Answered On: Jul 31

I have no clue i am just following the book.so what you saying is that i should
make everything public.. e.g
public Node top;
public int size;

I tired that too.. but.. do do i implement array method it says.. a.lenght i
cant use in my method.

 
Answer #29    Answered By: Cais Nguyen     Answered On: Jul 31

Well for many reasons, for instance the Singleton
pattern which some of the other members have pointed
out, but I use it to simulate in C/C++'s way of doing
const.

For instance, if I want to create a color object that
contains the Red, Green, and Blue values, I would have
to do this in Java.

public class Color
{
private int red;
private int green;
private int blue;

private Color( int red, int green, int blue )
{
this.red = red;
this.green = green;
this.blue = blue;
}

public Color RED = new Color( 255, 0, 0 );
public Color GREEN = new Color( 0, 255, 0 );
public Color BLUE = new Color( 0, 0, 255 );
public Color WHITE = new Color( 0, 0, 0 );
public Color BLACK = new Color( 255, 255, 255 );

// Public getter methods
public ...

// Do not include setter methods because we don't
want to change the object's data
}

So now I can do the following in other places

Color red = Color.RED;
Color black = Color.BLACK;

 
Didn't find what you were looking for? Find more on Why we declare the constructor private? Or get search suggestion and latest updates.




Tagged: