Class with no constructors

The class javax.microedition.rms .RecordStore does not have a constructor. Why?
Please note that I am not asking how to produce an instance of a RecordStore, I have read the documentation and am aware of the static openRecordStore method.
Paul Hide

javax.microedition.rms.RecordStore has no public or protected constructor so you can't extend it.
If a private constructor is defined in a class then it is not exposed in documentation (usually), but at the other hand if you have not defined any constructor then non-argument public constructor is shown in the documentation.
So it seems that javax.microedition.rms.RecordStore has private constructor.
So you can't extend it.
Following two type of classes are non-extendable:
- Whose all constructors are made private. E.g. java.lang.Runtime, java.lang.System.
- Final class (just final keyworkd is added in access specifiers). E.g. java.lang.String
Following two type of classes are non-instantiable:
- Whose all constructors are made private. These can be instantiated inside the class. E.g. java.lang.Runtime, java.lang.System
- Abstract class (just abstract keyword is added in access specifiers). E.g. java.io.OutputStream
Regards,
Humayun.

Similar Messages

  • Why can't classes with private constructors be subclassed?

    Why can't classes with private constructors be subclassed?
    I know specifying a private nullary constructor means you dont want the class to be instantiated or the class is a factory or a singleton pattern. I know the workaround is to just wrap all the methods of the intended superclass, but that just seems less wizardly.
    Example:
    I really, really want to be able to subclass java.util.Arrays, like so:
    package com.tassajara.util;
    import java.util.LinkedList;
    import java.util.List;
    public class Arrays extends java.util.Arrays {
        public static List asList(boolean[] array) {
            List result = new LinkedList();
            for (int i = 0; i < array.length; i++)
                result.add(new Boolean(array));
    return result;
    public static List asList( char[] array) {
    List result = new LinkedList();
    for (int i = 0; i < array.length; i++)
    result.add(new Character(array[i]));
    return result;
    public static List asList( byte[] array) {
    List result = new LinkedList();
    for (int i = 0; i < array.length; i++)
    result.add(new Byte(array[i]));
    return result;
    public static List asList( short[] array) {
    List result = new LinkedList();
    for (int i = 0; i < array.length; i++)
    result.add(new Short(array[i]));
    return result;
    public static List asList( int[] array) {
    List result = new LinkedList();
    for (int i = 0; i < array.length; i++)
    result.add(new Integer(array[i]));
    return result;
    public static List asList( long[] array) {
    List result = new LinkedList();
    for (int i = 0; i < array.length; i++)
    result.add(new Long(array[i]));
    return result;
    public static List asList( float[] array) {
    List result = new LinkedList();
    for (int i = 0; i < array.length; i++)
    result.add(new Float(array[i]));
    return result;
    public static List asList( double[] array) {
    List result = new LinkedList();
    for (int i = 0; i < array.length; i++)
    result.add(new Double(array[i]));
    return result;
    // Now that we extend java.util.Arrays this method is not needed.
    // /**JCF already does this so just wrap their implementation
    // public static List asList(Object[] array) {
    // return java.util.Arrays.asList(array);
    public static List asList(Object object) {
    List result;
    Class type = object.getClass().getComponentType();
    if (type != null && type.isPrimitive()) {
    if (type == Boolean.TYPE)
    result = asList((boolean[])object);
    else if (type == Character.TYPE)
    result = asList(( char[])object);
    else if (type == Byte.TYPE)
    result = asList(( byte[])object);
    else if (type == Short.TYPE)
    result = asList(( short[])object);
    else if (type == Integer.TYPE)
    result = asList(( int[])object);
    else if (type == Long.TYPE)
    result = asList(( long[])object);
    else if (type == Float.TYPE)
    result = asList(( float[])object);
    else if (type == Double.TYPE)
    result = asList(( double[])object);
    } else {
    result = java.util.Arrays.asList((Object[])object);
    return result;
    I do not intend to instantiate com.tassajara.util.Arrays as all my methods are static just like java.util.Arrays. You can see where I started to wrap asList(Object[] o). I could continue and wrap all of java.util.Arrays methods, but thats annoying and much less elegant.

    Why can't classes with private constructors be
    subclassed?Because the subclass can't access the superclass constructor.
    I really, really want to be able to subclass
    java.util.Arrays, like so:Why? It only contains static methods, so why don't you just create a separate class?
    I do not intend to instantiate
    com.tassajara.util.Arrays as all my methods are static
    just like java.util.Arrays. You can see where I
    started to wrap asList(Object[] o). I could continue
    and wrap all of java.util.Arrays methods, but thats
    annoying and much less elegant.There's no need to duplicate all the methods - just call them when you want to use them.
    It really does sound like you're barking up the wrong tree here. I can see no good reason to want to subclass java.util.Arrays. Could you could explain why you want to do that? - perhaps you are misunderstanding static methods.
    Precisely as you said, if they didn't want me to
    subclass it they would have declared it final.Classes with no non-private constructors are implicitly final.
    But they didn't. There has to be a way for an API
    developer to indicate that a class is merely not to be
    instantiated, and not both uninstantiable and
    unextendable.There is - declare it abstract. Since that isn't what was done here, I would assume the writers don't want you to be able to subclass java.util.Arrays

  • Default class with public constructor - why?

    Noticed some code that had a class with package-level access and a public constructor - wondered what the benefit/use of that would be... Thanks!

    GarudaJava wrote:
    Noticed some code that had a class with package-level access and a public constructor - wondered what the benefit/use of that would be... Thanks!If the class itself doesn't need to be exposed, but it implements some interface, and is assumed to have a public no-arg consturctor, it could be created reflexively. This is a contrived situation, but it's the only one I can think of in Java where that'd be useful.
    package foo;
    class Foo implements SomeInterface {
      public Foo() {
    package factory;
    class Factory {
      public static SomeInterface create(Class<? extends SomeInterface> clazz) { // not sure if I got the generics right, but they're incidendtal to the example
        return clazz.newInstance();
    package foo;
    import factory.Factory;
    public class Bar {
      public void bar() {
        SomeInterface si = Factory.create(Foo.class);
        si.doStuff();
    }Like I said, pretty contrived, and I can't think of a real-world use case that matches it offhand, but structurally it'd look something like that.
    You could also maybe imagine that the factory package might do more than just create and return an instance. It might create it and use that SomeInterface type for its own ends.

  • Class with private constructor can be extended or not

    Hi All,
    I have a doubt.
    if a class has private constructor and there are some methods in this class.Can this class be extended and if yes how can we call its method in subclass?
    Thanks
    Sumit

    Karanjit wrote:
    If a class contains only private constructors, then it cannot be extended.Err... not the whole story!
    public class Sabre20090603a
        static class Fred extends Sabre20090603a
            Fred()
                super();
        private Sabre20090603a()
    }

  • Private inner class with private constructor

    I read that if constructor is public then you need a static method to create the object of that class.
    But in the following scenario why I am able to get the object of PrivateStuff whereas it has private constructor.
    I am messing with this concept.
    public class Test {
          public static void main(String[] args) {          
               Test t = new Test();
               PrivateStuff p = t.new PrivateStuff();
          private class PrivateStuff{
               private PrivateStuff(){
                    System.out.println("You stuff is very private");
    }

    A member (class, interface, field, or method) of a reference (class, interface, or array) type or a constructor of a class type is accessible only if the type is accessible and the member or constructor is declared to permit access:
    * Otherwise, if the member or constructor is declared private, then access is permitted if and only if it occurs within the body of the top level class (§7.6) that encloses the declaration of the member or constructor. [Java Language Specification|http://java.sun.com/docs/books/jls/third_edition/html/names.html#6.6.1]
    Your main method is within the body of the top level class, so the type (the inner class) and method are accessible.
    eg:
    class ImInTheSameSourceFileAsInnerAccessTest {
        public static void main(String[] args) {          
            InnerAccessTest t = new InnerAccessTest();
            InnerAccessTest.PrivateStuff p = t.new PrivateStuff();
    public class InnerAccessTest {
          public static void main(String[] args) {          
               InnerAccessTest t = new InnerAccessTest();
               PrivateStuff p = t.new PrivateStuff();
          private class PrivateStuff{
               private PrivateStuff(){
                    System.out.println("You stuff is very private");
    }Result:
    $ javac -d bin src/InnerAccessTest.java
    src/InnerAccessTest.java:4: InnerAccessTest.PrivateStuff has private access in InnerAccessTest
    InnerAccessTest.PrivateStuff p = t.new PrivateStuff();
    ^
    src/InnerAccessTest.java:4: InnerAccessTest.PrivateStuff has private access in InnerAccessTest
    InnerAccessTest.PrivateStuff p = t.new PrivateStuff();
    ^
    2 errors
    Edited by: pm_kirkham on 20-Jan-2009 10:54 added example of 'in the same source file'

  • Creating a generic class with a constructor that takes an array of objects

    I am relatively new to java and want to build a quick utility class that can generate a Run Length Encoding of any object. The idea is to take an array of objects of type "O" and then encode a string of objects that are "equal" to each other as an integer that counts the number of such equal instances and a single copy of the instance. This has the potential to very quickly reduce the size of some objects that I want to store. I would like to implement this class as a generic.
    public class RunLengthEncoding<O> {
         private class RLEPair {
              private int length;
              private O object;
         public RunLengthEncoding(O[]) {
    }As you can see, I need to make a constructor that takes an array of type "O". Is this possible to do? I can't seem to find the right syntax for it.
    Thanks,
    Sean

    Sorry. Obvious answer:
    public RunLengthEncoding(O[] oarray) {Again, sorry for the noise.

  • Creating an instance of a class with no default constructor

    Hello gurus,
    I wrote my own serialization and RMI protocol for both C++ and Java that follows closely what the default Java version does. I'm trying to recreate an object on the Java side that was sent over the wire. The first step is to create an instance of the class. How do I create an instance of a class that has no constructor (i.e. the only instances are static, created by the class itself and returned by static methods) or one that has no default constructor (like Integer)? The Java serialization seems to support it but the reflection API doesn't seem to have any support for this (i.e. Class::newInstance() and Constructor::newInstance()). It seems that through the standard API you can only create an object via one of its constructors. There must be a "hidden" method somewhere that allows the Java serialization to create an object without calling a constructor - where is it?
    Dominique

    There must be a "hidden" method
    somewhere that allows the Java serialization to create
    an object without calling a constructor - where is
    it?You are correct, the way in which the Serialization creates Objects is "hidden" deep within the runtime.
    If it were not hidden, you would be able to find it, and use it to violate the integrity of the VM.

  • Instantiate class with a private constructor

    How do you call a class that happens to have a private constructor. how would one instantiate such a class? Generally constructors are supposed to be public.
    But in many cases the constructor for a class can be private, so how to call such a class?

    khurrumnas wrote:
    I dont think you would need to provide such a method (newInstance()), its there for you already in Class Class, so its suffice to just call
    newInstance() to get an instance of class that happens to have a private constructor, in the simplest case.False, as jverd rightly mentioned. Example: public class Foo {
        public static void main(String[] args) {
            Exception expectedException = null;
            try {
                Bar b = (Bar) Class.forName("Bar").newInstance(); // throws Exception
            } catch (Exception e) {
                expectedException = e;
            assert expectedException instanceof IllegalAccessException;
    class Bar {
        private Bar() {}
    }As mentioned several times now, one can get around this restriction, but it's considered poor form to do so.
    ~

  • Having problems in creating a class with name File.

    I want to compile a new class with class name as File, i have made it part of a new package also but still i am getting this error
    "File.java:2: CustomFile.File is already defined in this compilation unit
    import java.io.File;
    ^
    File.java:6: cannot resolve symbol
    symbol : constructor File ()
    location: class java.io.File
    inFile = new java.io.File();
    ^
    2 errors"
    Here is the source code of the file:
    package CustomFile;
    import java.io.File;
    public class File {
    private java.io.File inFile;
    public File(){
         inFile = new java.io.File();
         System.out.println("hi file");
    }

    Any help is appreciated.
    I want to compile a new class with class name as File,
    i have made it part of a new package also but still i
    am getting this error
    "File.java:2: CustomFile.File is already defined in
    this compilation unit
    import java.io.File;
    ^
    File.java:6: cannot resolve symbol
    symbol : constructor File ()
    location: class java.io.File
    inFile = new java.io.File();
    ^
    2 errors"
    Here is the source code of the file:
    package CustomFile;
    import java.io.File;
    public class File {
    private java.io.File inFile;
    public File(){
         inFile = new java.io.File();
         System.out.println("hi file");

  • Problems with java constructor: "inconsistent data types" in SQL query

    Hi,
    I tried to define a type "point3d" with some member functions, implemented in java, in my database. Therefor I implemented a class Point3dj.java as you can see it below and loaded it with "loadjava -user ... -resolve -verbose Point3dj.java" into the database.
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    package spatial.objects;
    import java.sql.*;
    public class Point3dj implements java.sql.SQLData {
    public double x;
    public double y;
    public double z;
    public void readSQL(SQLInput in, String type)
    throws SQLException {
    x = in.readDouble();
    y = in.readDouble();
    z = in.readDouble();
    public void writeSQL(SQLOutput out)
    throws SQLException {
    out.writeDouble(x);
    out.writeDouble(y);
    out.writeDouble(z);
    public String getSQLTypeName() throws SQLException {
    return "Point3dj";
    public Point3dj(double x, double y, double z)
    this.x = x;
    this.y = y;
    this.z = z;
    public static Point3dj create(double x, double y, double z)
    return new Point3dj(x,y,z);
    public double getNumber()
         return Math.sqrt(this.x*this.x + this.y*this.y + this.z*this.z);
    public static double getStaticNumber(double px, double py, double pz)
         return Math.sqrt(px*px+py*py+pz*pz);
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Additionally, I created the corresponding type in SQL by
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    CREATE OR REPLACE TYPE point3dj AS OBJECT EXTERNAL NAME
    'spatial.objects.Point3dj' LANGUAGE JAVA USING SQLDATA (
    x FLOAT EXTERNAL NAME 'x',
    y FLOAT EXTERNAL NAME 'y',
    z FLOAT EXTERNAL NAME 'z',
    MEMBER FUNCTION getNumber RETURN FLOAT
    EXTERNAL NAME 'getNumber() return double',
    STATIC FUNCTION getStaticNumber(xp FLOAT, yp FLOAT, zp FLOAT) RETURN FLOAT
    EXTERNAL NAME 'getStaticNumber(double, double, double) return double')
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    After that I tried some SQL commands:
    create table pointsj of point3dj;
    insert into pointsj values (point3dj(2,1,1));
    SELECT x, a.getnumber() FROM pointsj a;Now, the problem:
    Everything works fine, if I delete the constructor
    public Point3dj(double x, double y, double z)
    this.x = x;
    this.y = y;
    this.z = z;
    in the java class, or if I replace it with a constructor that has no input arguments.
    But with this few code lines in the java file, I get an error when executing the SQL command
    SELECT x, a.getnumber() FROM pointsj a;The Error is:
    "ORA-00932: inconsistent data types: an IN argument at position 1 that is an instance of an Oracle type convertible to an instance of a user defined Java class expected, an Oracle type that could not be converted to a java class received"
    I think, there are some problems with the input argument of the constructor, but why? I don't need the constructor in SQL, but it is used by a routine of another java class, so I can't just delete it.
    Can anybody help me? I would be very glad about that since I already tried a lot and also search in forums and so on, but wasn't successful up to new.
    Thanks!

    Dear Avi,
    This makes sense when it is a short code sample (and i think i've done that across various posts), but sometime this is too long to copy/paste, in these cases, i refer to the freely available code samples, as i did above on this forum; here is the quote.
    Look at examples of VARRAY and Nested TABLES of scalar types in the code samples of my book (chapter 8) http://books.elsevier.com/us//digitalpress/us/subindex.asp?maintarget=companions/defaultindividual.asp&isbn=9781555583293&country=United+States&srccode=&ref=&subcode=&head=&pdf=&basiccode=&txtSearch=&SearchField=&operator=&order=&community=digitalpress
    As you can see, i was not even asking people to buy the book, just telling them where to grab the code samples.
    I appreciate your input on this and as always, your contribution to the forum, Kuassi

  • Object with argument constructor in ATG

    HI guys
    Is it possible to craete an object with argument constructor with component in atg
    if it is possible give me an example

    With ATG 10.0.3 and later, you can use an $instanceFactory to create objects with constructor objects or create a Nucleus component from factory (either a static class method, or a method of another Nucleus component).
    Documentation (and an example) are here: http://docs.oracle.com/cd/E36434_01/Platform.10-1-2/ATGPlatformProgGuide/html/s0208parameterconstructorinstancefact01.html

  • Need to instantiate a class with the contents of a string variable

    I have:
    String temp = "hello";
    and I want to instantiate a class with the contents of the variable temp. So what I want is:
    Class hello = new Class();
    Does anybody know how to do this?

    I'm getting kind of confused with decifering what
    people are writing. I'm not real sure what the Java
    syntax is and what I should be modifying for my code.
    Basically, I have created a class called Artist. I
    want to parse through a text file and when I see a
    header field called "artist", my program will
    instantiate the artist class with the next String
    token. For example, if my text file reads:
    --artist:Daniel
    I want to create an Artist object with the name
    Daniel. Ideally I could just use the code:
    --Artist Daniel = new Artist();I cannot use this syntax because I don't know that "Daniel" is going to follow "artist" in the text file. It could be a different name or String
    >
    But the way I'm going about it right now is to store
    "Daniel" in a variable called temp by:
    --//"in" is an instantiation of StreamTokenizer
    --String temp = in.sval;    
    Right now I have the Artist class set up so there is
    just a default constructor.

  • Singleton Class using a constructor not a getInstance method

    Hi,
    I have a class with a couple of contructors, the class is instantiated many times in the application.
    Is there any way by which I can return a single instance of the class when the contructor is called ?.
    I dont want to declare a get instance method to return a single instance is it somehow possible to do this in the contructor itself.
    This is done so I dont have to refactor the whole application and change all the places where this class is instantiated.
    Example
    XXXClass xxx = new XXXClass();
    every time new XXXClass() is called the single instance of the class must be returned.
    I dont want to implement
    XXXClass xxx = XXXClass.getInstance();
    Thanks in advance
    Rgds
    Pradeep Thomas

    The short answer to your question is No. If your class has accessible constructors and your code calls "new" then there will be multiple instances of the corresponding objects.
    Possible ways round your problem are to use static variables in place of instance variables or to use your existing class as a wrapper for another class that is instantiated only once. This second class should include all the variables and methods and the wrapper class would delegate all calls to it. This is not quite a singleton because there would still be multiple instances of the wrapper, but each instance would share the same variables and methods. You could also add a getInstance() method to the wrapper class so that new code could start to use it as a singleton.
    Other than that, it's time to refactor! Good Luck.

  • Singleton Class using a constructor not a get Instance method ?

    Hi,
    I have a class with a couple of contructors, the class is instantiated many times in the application.
    Is there any way by which I can return a single instance of the class when the contructor is called ?.
    I dont want to declare a get instance method to return a single instance is it somehow possible to do this in the contructor itself.
    This is done so I dont have to refactor the whole application and change all the places where this class is instantiated.
    Example
    XXXClass xxx = new XXXClass();
    every time new XXXClass() is called the single instance of the class must be returned.
    I dont want to implement
    XXXClass xxx = XXXClass.getInstance();
    Thanks in advance
    Rgds
    Pradeep Thomas

    Pradeep ,
    You see a constructor does not return anything
    when you tell java
    String str = new String();
    It is into the reference str that a Object of the type String is assigned and used ..
    If you want a single instance to exist in your application ..the best way is to use the singelton approach.
    Alternatively you could create one static instance of the object in a parent class and all the classes that inherit from it will then share access .

  • Accessing inner protected class with .new

    My weak understanding is that inner classes are no different in terms of access modification as other class members, thus I was expecting the following to compile:
    package A;
    public class ClassA {
        protected void protectedMethod() {}
        protected class ProtectedInnerClass {}
    }is being used over here in package B
    package B;
    import A.*;
    public class ClassB extends ClassA {
    public static void main(String[] args) {
    ClassB b = new ClassB();
    b.protectedMethod(); // works fine. I am inheriting ClassA and calling the protected method.
    ClassB.ProtectedInnerClass c = b.new ProtectedInnerClass(); // compiler error: ProtectedInnerClass is protected
    }Why can I not access the protected inner class when ClassB is inheriting it as is the case with the protected method?
    thanks in advance!

    So classes that inherit other classes with protected inner classes will not be able to instantiate these inner classes regardless of the inheritance relationship.No, that's not true. Example:
    package A;
    public class ClassA {
         protected void protectedMethod() {
         protected class ProtectedInnerClass { // class is protected
              public ProtectedInnerClass() { } // <-- nota bene
    package B;
    import A.ClassA;
    public class ClassB extends ClassA {
         public static void main(String[] args) {
              ClassB b = new ClassB();
              b.protectedMethod(); // works fine.
              ClassB.ProtectedInnerClass c = b.new ProtectedInnerClass(); // works fine, too.
    }It's section 6.6.2.2 that describes this behavior, as I quoted above. The last bullet point of that section reads (to repeat): "A protected constructor can be accessed by a class instance creation expression (that does not declare an anonymous class) only from within the package in which it is defined."
    ~

Maybe you are looking for

  • Mac Mini 2011 screen blacks out during heavy screen activity

    I have a Mac Mini 2011 i5 2.3Ghz with Intel HD video.  It's connected to a 30" LCD 2560x1600 (Dell WFP3008) via mini-DP to DP cable and a 22" LCD 1920x1080 (Samsung LCD TV) via HDMI to HDMI cable. For the first few weeks I had it connected to the 30"

  • How to determine the field size

    I am going to make a multiplatform application that hopefully will run on linux and windows 2000.If the os is 2000, then I will use vb.net/aspx else I'll use java servlets. I make the connection to the web server ( through HTTP) not directly to datab

  • Plug Icon Won't Go Away - Help !

    I did the software update for my 20GB Color iPod. Then it wanted me to plug it in, as the icon indicated. Unfortunately, that's not doing anything at all. For goodness sakes, this is the 3rd time I've had an iPod die like this in a year. Arghhh ! Ple

  • Should I organise on import or later?

    At the moment most of my photos are not in iPhoto, they are sort organised in folders by date/events, eg Holiday 10, Xmas 11 etc I have recently joined Crashplan, although I also use Time Machine. So at the moment CrashPlan isn't backing up them fold

  • Need help on java with mysql...

    I want to make application that will use mysql as database. I have downloaded mysql connector j but i'm confuse about how to install it to jdk. for reference, i'm using windows xp, phptriad that have mysql 4.1. After that, how do I connect my applica