Final class and private constructor

Whats the difference in a final class and a class with private constructot?
If we can make a class non-extendable by just giving private constructor then whats the advantage of final class? (I know final is very useful for many other things but just want to get info in this contaxt)

You can extend a class with a private constructor,I'm not sure about that. The compiler will complain.
KajThat depends on the signature of the private constructor.
If it's the no-arg constructor and you don't declare another constructor which you explicitly call from your derived class you will indeed get an error.
If however you never call it (and there's no way it can implicitly get called) from a derived class there should be no problem.
class Private1 {
     private int q;
     private Private1() {
     public Private1(int i) {
          q = i;
     public void print() {
          System.out.println(q);
public class Public1 extends Private1 {
     public Public1() {
          super(1);
     public static void main(String[] args) {
          new Public1().print();
}for example compiles and runs perfectly.
But remove the call "super(1);" from the constructor and it will fail to compile.

Similar Messages

  • Help: Factory Class using Inner Class and Private Constructor?

    The situation is as follows:
    I want a GamesCollection class that instantiates Game objects by looking up the information needed from a database. I would like to use Game outside of GamesCollection, but only have it instantiated by GamesCollection to ensure the game actually exist. Each Game object is linked to a database record. If a Game object exist, it must also exist in the database. Game objects can never be removed from the database.
    I thought about making the Game object an inner class of GamesCollection, but this means that Game class constructor is still visible outside. So what if I made Game constructor private? Well, now I can't create Game objects without a static method inside Game class (static Object factory).
    Basically what I need is a constructor for the inner Game class accessible to GamesCollection, but not to the rest of the world (including packages). Is there a way to do this?

    leesiulung wrote:
    As a second look, I was initially confused about your first implementation, but it now makes more sense.
    Let me make sure I understand this:
    - the interface is needed to make the class accessible outside the outer classBetter: it is necessary to have a type that is accessible outside of GameCollection -- what else could be the return type of instance?
    - the instance() method is the object factory
    - the private modifier for the inner class is to prevent outside classes to instantiate this objectRight.
    However, is a private inner class accessible in the outer class? Try it and see.
    How does this affect private/public modifiers on inner classes?Take about five minutes and write a few tests. That should answer any questions you may have.
    How do instantiate a GameImpl object? This basically goes back to the first question.Filling out the initial solution:
    public interface Game {
        String method();
    public class GameCollection {
        private static  class GameImpl implements Game {
            public String method() {
                return "GameImpl";
        public Game instance() {
            return new GameImpl();
        public static void main(String[] args) {
            GameCollection app = new GameCollection();
            Game game = app.instance();
            System.out.println(game.method());
    }Even if you were not interested in controlling game creation, defining interfaces for key concepts like Game is always going to be a good idea. Consider how you will write testing code, for example. How will you mock Game?

  • 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

  • Enclosing class calling private constructor of private member class

    Hi all,
    I have this question concerning member classes and privtae constructors.
    public class MyTest {
        private class Inner {
            private Inner() {
                System.out.println("Why Am I here!??");
        public MyTest() {
            Inner a = new Inner();
        public static void main(String[] s) {
            MyTest z = new MyTest();
    }It doesn't work for my JDK SE 1.3.3, build 1.3.1-b24.
    It works for many other versions.
    Can somebody kindly enlighten me, should this code work?
    I really didn't think that it should, but it did!

    I am sorry. It was actually my jikes 1.15 that was causing the problem.
    After some research, I found out that my problem arose out of my understanding of OO concepts, or rather, the meaning of access modifiers in Java.
    I had thought that nobody can access a private variable/method of class except the class itself. Apparently, this is not so. The access modifiers apply to the class themselves and not the object. Thus explaining why an object can access the private variables/methods of another object of the same class.
    Actually, it's not really the case here. The Java language specs states that the inner class has total access to the enclosing class, but I could not find any word on enclosing class access to inner classes in the specs.
    As for Jikes, I really hope they will fix it soon. I like it a lot as it is significantly faster than javac for everything I have done so far.
    cheers!

  • 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'

  • Abstract Classes vs Private Constructor

    Hopefully this doesn't make me sound to naive
    If we have a super class called food, and subclasses chocolate, chicken, cereal and so on then its clear we dont want to make instances of the super class food correct? So we can make this class an abstract class
    What are the benefits of making a class abstract over making a constructor private and in what circumstance would it be beneficial to do one over the other?
    Thanks, and sorry for a poor example!
    Edited by: compSciUndergrad on Apr 22, 2009 4:53 AM

    wrybread wrote:
    Also, Corlettk -
    Excellent point regarding prudent use of interfaces; thank you. If you read this, I'd appreciate any comment you have on how conservative programmers need to be in designing an interface. It seems to me that if I'm uncertain whether or not to include a certain member in the interface, I should omit it or include it elsewhere (perhaps in an interface all its own, or in an abstract super if that's applicable).
    Let me know if you want me to open this as a new thread; I'm just looking for your take on this topic. -- Thank you!Obviously, there's no right answer. Each deisgn must be assessed on a case-by-case basis. Every design presents new and interesting problems.
    I'm no OO guru... what I do know is that you can make some very nasty mistakes, thanks to experience of maintaining a fairly poorly written (first in the organisation) large java project.
    I'm of the opinion that interface should define an aspect of behaviour... If the Sun GODS had there time again they'd do java.io would be full of interfaces... Closeable, Openable, Readable, Writable, Printable... etc etc etc.
    Please remember that a class (or group of classes) can implement multiple interfaces... and an interface can extend multiple interfaces... So java supports "multiple type inheritance" as apposed to C++'s "multiple implementation inheritance".
    If you need an "aglomerate" interface as the type for a collection then assemble one from your premade "set of aspects"... eg: Input extends Openable, Closeable, Readable...
    package forums;
    interface AnInterface
      public void a();
    interface AnotherInterface
      public void b();
    interface AglomerateInterface extends AnInterface, AnotherInterface
      public void c();
    class AglomerateImpl implements AglomerateInterface
      public static void main(String[] args) {
        try {
          System.out.println("Hello World!");
        } catch (Exception e) {
          e.printStackTrace();
      public void a() {}
      public void b() {}
      public void c() {}
    }PS: You wouldn't normally define multiple interfaces and an implementation all in one file... sort of defeats the whole purpose really... and in fact you can't as soon as you make the interfaces public, which they need to be to be any use.
    Make sense?

  • Question  about Abstract,Final Class

    when we are using final keyword along with class definition .we are making that class final, means we can’t extend that class. The same thing happens when we make our constructors private. From usability perspective both are same ? or is there any difference?
    Secondly accounting to java syntax a class can be either final or abstract and not both .Then why language specification doesn't impose any restriction on making abstract classes constructor private. We can create an Abstract class with private Constructor (Basically utility class with all methods are static) ,to make this class Singleton .This situation is equal to abstract final class ?
    Thanks,
    Paul.

    EJP wrote:
    when we are using final keyword along with class definition .we are making that class final, means we can’t extend that class. The same thing happens when we make our constructors private.No it doesn't.
    Secondly accounting to java syntax a class can be either final or abstract and not both.Correct.
    Then why language specification doesn't impose any restriction on making abstract classes constructor private.Why should it? That proposition doesn't follow from your previous sentence.I think OP is asking about this case
    public abstract class WTF {
      private WTF() {}
      private WTF(...) {}
    }where the class is abstract and all the c'tors are final. It's an abstract class that cannot be extended, much like a final abstract class would be if it were allowed. So, since purpose of abstract classes is to be extended, the OP seems to be asking, "Why can we, in this fashion, create an abstract class that cannot be extended?"
    I don't know the answer to that, but I would guess that, while final is an explicit syntactical element for the purpose of making a class non-extensible, in the all-private-c'tors case, the non-extensibility is simply a side effect, and probably enough of a corner case that it wasn't worth the effort to explicitly forbid it in the spec.
    I also think it's something not worth thinking much about. It was certainly not a key point of some grand design.

  • Overidding a method defined in a Final class via an interface.

    Hi,
    Is it possible to override methods of a final class
    which has implemented methods of an interface.
    interface A{
         void method1();     
    public final class FinalityA implements A{
         public void method1(){
              // Code
    }Now,I would like to override the functionality of
    method1() defined in class FinalityA.
    I cannot subclass FinalityA as this class is final.
    // Invalid.
    class Myclass extends FinalityA{
    }Is it possible to override the functionality defined in method1()
    Please suggest

    You can do:
    public class MyClass implements A {
       private final FinalityA _wrapee;
       public MyClass(FinalityA wrapped) {
           _wrapee = wrapped;
      public void method1() {
           .. do some special processing
          _wrapee.method1();
           .. do some more stuff
            }If you want to get really clever you can do stuff with java.lang.reflect.Proxy that allows you to automatically pass on calls to interface methods (this kind of thing is increasingly used for stuff like transaction management).
    What you can't do is to fiddle with the code of the method if called from a reference to FinalityA rather than a reference to the interface. The whole point of final classes and methods is to prevent that.

  • Abstract class & final class

    Which is more correct... Or which is the more preferred way.
    abstract class A
      final method1(){}
      final method2(){}
      final method30(){}
    }or
    final class A
      private A(){}
      method1(){}
      method2(){}
      method3(){}
    }My understanding is that both classes cannot be instantiated. The first one requires writing 'final' for EACH method. The second one involves writing a private constructor.

    It depends on what you are trying to do. If you are trying to make a class that can be sub-classed but has some methods that cannot be overriden, then "abstract class A" with final methods is the way to go. If you want a class that cannot be sub-classed or instantiated then "final class A" with private constructor is the way to go.
    classes cannot be instantiatedOnly true for "final class A" because you made the only constructor private. Not true for the abstract one.// you forgot return values for the methods
    abstract class A
      final static /*void*/ method1(){}  // package private
      final static /*void*/ method2(){}  // package private
      final static /*void*/ method30(){} // package private
    // this would work
    A a = new A(){};
    // if I am in the same package as A, then this would work
    a.method1();
    // or this
    public class B extends A
      public B(String whatever)
        // Although, I cannot override the super methods
        // becuase they are all declared as final
        // I can only invoke them if I am in the same package.  You
        // declared them as package private instead of class "private"
    }Using final as a class modifier disables the ability to sub-class it but does not disable the ability to create an object of that class. You must make a private constructer. If the only constructor is "private" then you can't subclass or instantiate, so making the class final is uneeded.

  • How do I redefine a Method of a Final Class?

    Hi,
    Is it possible to redefine a method of a final class and if so, can someone please give me a brief example of that technique?
    Thank you very much!
    Andy

    Hi,
    Please find the example.
    Program Description :      Interface I1 contains two methods : M1 and M2.
    I1 is included and incorporated in class : C1 with M2 as a final method. Both the methods are implemented in class C1.
    Class C2 is a subclass of class C1. It redefines method : I1M1 and re-implements it, but it does not do that for I1M2 as that is declared as final method.
    In the START-OF-SELECTION block, object OREF1 is created from class C1 and OREF2 from class C2 and both the methods M1 and M2 are called using both the objects.
    Example:
    report ytest .
    interface i1 .
      methods : m1 ,
                m2 .
    endinterface.
    class c1 definition.
      public section.
       interfaces : I1 final methods m2 .
    endclass.
    class c1 implementation.
      method i1~m1.
       write:/5 'I am m1 in c1'.
      endmethod.
      method i1~m2.
       write:/5 'I am m2 in c1'.
      endmethod.
    endclass.
    class c2 definition inheriting from c1.
      public section.
       methods : i1~m1 redefinition .
    endclass.
    class c2 implementation.
      method : i1~m1.
       write:/5 'I am m1 in c2'.
      endmethod.
    endclass.
    start-of-selection.
      data : oref1 type ref to c1,
             oref2 type ref to c2 .
       create object : oref1 , oref2.
       call method  : oref1->i1~m1 , u201C Output : I am m1 in c1
                      oref2->i1~m1 , u201C Output : I am m1 in c2
                      oref1->i1~m2 , u201C Output : I am m2 in c1
                      oref2->i1~m2 . u201C Output : I am m2 in c1
    Output :      I am m1 in c1 
    I am m1 in c2 
    I am m2 in c1 
    I am m2 in c1 
    Thanks,
    Anitha

  • Anonymous classes and non-default constructors

    I've got a class with only one constructor and that takes an argument. In another class, I want to have an anonymous class that extends this class with something like:
    new MyClassWithoutDefaultConstructor(myConstuctorArg) {...}
    However, I get a "The constructor MyClassWithoutDefaultConstructor() is undefined".
    As a workaround I can create a local class (not anonymous) that extends MyClassWithoutDefaultConstructor and then includes a default constructor which passes my arg to the super constructor. But this is rather messy.
    Am I missing something?

    The following works fine for me (prints 5):
    public abstract class Test
        private final int parameter;
        public Test(int parameter)
            this.parameter=parameter;
        public int getParameter()
            return parameter;
        public abstract int getSomething();
        public static void main(String[] args)
            Test test=new Test(3)
                public int getSomething()
                    return getParameter()+2;
            System.out.println(test.getSomething());
    }You say your anonymous class is in a different class to the one it extends - what is the access modifier on the constuctor you are calling in the base class? Is the constructor visible from the class containing the anonymous class? Can you post a concise example that produces the compiler error that you are getting?

  • Problem with final variables and inner classes (JDK1.1.8)

    When using JDK1.1.8, I came up with following:
    public class Outer
        protected final int i;
        protected Inner inner = null;
        public Outer(int value)
            i = value;
            inner = new Inner();
            inner.foo();
        protected class Inner
            public void foo()
                System.out.println(i);
    }causing this:
    Outer.java:6: Blank final variable 'i' may not have been initialized. It must be assigned a value in an initializer, or in every constructor.
    public Outer(int value)
    ^
    1 error
    With JDK 1.3 this works just fine, as it does with 1.1.8 if
    1) I don't use inner class, or
    2) I assign the value in initializer, or
    3) I leave the keyword final away.
    and none of these is actually an option for me, neither using a newer JDK, if only there is another way to solve this.
    Reasons why I am trying to do this:
    1) I can't use a newer JDK
    2) I want to be able to assign the variables value in constructor
    3) I want to prevent anyone (including myself ;)) from changing the value in other parts of the class (yes, the code above is just to give you the idea, not the whole code)
    4) I must be able to use inner classes
    So, does anyone have a suggestion how to solve this problem of mine? Or can someone say that this is a JDK 1.1.8 feature, and that I just have to live with it? In that case, sticking to solution 3 is probably the best alternative here, at least for me (and hope that no-one will change the variables value). Or is it crappy planning..?

    You cannot use a final field if you do not
    initialize it at the time of declaration. So yes,
    your design is invalid.Sorry if I am being a bit too stubborn or something. :) I am just honestly a bit puzzled, since... If I cannot use a final field in an aforementioned situation, why does following work? (JDK 1.3.1 on Linux)
    public class Outer {
            protected final String str;
            public Outer(String paramStr) {
                    str = paramStr;
                    Inner in = new Inner();
                    in.foo();
            public void foo() {
                    System.out.println("Outer.foo(): " + str);
            public static void main( String args[] ) {
                    String param = new String("This is test.");
                    Outer outer = new Outer(param);
                    outer.foo();
            protected class Inner {
                    public void foo() {
                            System.out.println("Inner.foo(): " + str);
    } producing the following:
    [1:39] % javac Outer.java
    [1:39] % java Outer
    Inner.foo(): This is test.
    Outer.foo(): This is test.
    Is this then an "undocumented feature", working even though it shouldn't work?
    However, I assume you could
    get by with eliminating the final field and simply
    passing the value directly to the Inner class's
    constructor. if not, you'll have to rethink larger
    aspects of your design.I guess this is the way it must be done.
    Jussi

  • Best practice for class and constructors

    I'm writing a small utility class that has a private member that is a List. I'm troubled when I think about where this List should be created - in the declaration of the reference or in the default constructor - thus having all other constructors call the default.
    Should I have:
    public Class Foo() {
        private List myList = new ArrayList();
      public Foo() {
      public Foo(String myFoo) {
      public Foo(String[] myFoo) {
      // more methods....
    }or would it be better to code it as:
    public Class Foo() {
        private List myList;
      public Foo() {
        myList = new ArrayList();
      public Foo(String myFoo) {
        foo();
       // other stuff
      public Foo(String[] myFoo) {
        foo();
       // other stuff
      // more methods....
    }In the first option, I know from putting a watch on myList that it stays undefined until any one of the constructors in called. This would seem to me to be the best approach as it doesn't require future additional constructors to have to be written to specifically call the default constructor just to get the ArrayList created. What's typically done in this situation? Which one is considered "good form" or is there a better way?

    I'm writing a small utility class that has a private
    member that is a List. I'm troubled when I think
    about where this List should be created - in the
    declaration of the reference or in the default
    constructor - thus having all other constructors call
    the default.It's not done this way - you have to call this().
    I think the better solution is to have the creation and initialization of the List in the most specific constructor, and have the default call that:
    public class Foo
        private List myList;
        public Foo()
            this(Collections.EMPTY_LIST);
        public Foo(List newList)
            this.myList = new ArrayList(newList);
    }%

  • Using Class and Constructor to create instances on the fly

    hi,
    i want to be able to create instances of classes as specified by the user of my application. i hold the class names as String objects in a LinkedList, check to see if the named class exists and then try and create an instance of it as follows.
    the problem im having is that the classes i want to create are held in a directory lower down the directory hierarchy than where the i called this code from.
    ie. the classes are held in "eccs/model/behaviours" but the VM looks for them in the eccs directory.
    i cannot move the desired classes to this folder for other reasons and if i try to give the Path name to Class.forName() it will not find them. instead i think it looks for a class called "eccs/model/behaviours/x" in the eccs dir and not navigate to the eccs/model/behaviours dir for the class x.
    any ideas please? heres my code for ye to look at in case im not making any sense:)
    //iterator is the Iterator of the LinkedList that holds all the names of the
    //classes we want to create.
    //while there is another element in the list.
    while(iterator.hasNext())
    //get the name of the class to create from the list.
    String className = (String) iterator.next();
    //check to see if the file exists.
    if(!doesFileExist(className))
    System.out.println("File cannot be found!");
    //breake the loop and move onto the next element in the list.
    continue;
    //create an empty class.
    Class dynamicClass = Class.forName(className);
    //get the default constructor of the class.
    Constructor constructor = dynamicClass.getConstructor(new Class[] {});
    //create an instance of the desired class.
    Behaviour beh = (Behaviour) constructor.newInstance(new Object[] {});
    private boolean doesFileExist(String fileName)
    //append .class to the file name.
    fileName += ".class";
    //get the file.
    File file = new File(fileName);
    //check if it exists.
    if(file.exists())
    return true;
    else
    return false;
    }

    ok ive changed it now to "eccs.model.behaviours" and it seems to work:) many thanks!!!
    by the following you mean that instead of using the method "doesFileExist(fileName)" i just catch the exception and throw it to something like the System.out.println() ?
    Why don't you simply try to call Class.forName() and catch the exception if it doesn't exist? Because as soon as you package up your class files in a jar file (which you should) your approach won't work at all.i dont think il be creating a JAR file as i want the user to be able to create his/her own classes and add them to the directory to be used in the application. is this the correct way to do this??
    again many thanks for ye're help:)

Maybe you are looking for

  • How do you set up an iphone 4s with no sim

    My iPhone 4s is updated to the i0s7.1. It won't let me set it up without a SIM!! How do I get it to work?????

  • How to organize the content of MANIFEST.MF

    Most of the jar files contain a file named META-INF/MANIFEST.MF, and it provides some information about the class in the jar files, but when I execute the code below, I can't output all of the jar file name in the eclipse project's classpath. import

  • Sound levels not consistent

    On my iphone 4S (and on my older 3Gs), i've noticed that sound effect levels (email alerts, SMS alerts, screen locking) are not consistent. Sometimes they are louder than other times, and sometimes they're not happening at all. I've got the volume up

  • Problem accessing mounted ntfs partitions

    Hi, I have problems accessing ntfs partitions as a non root user. The user trying to acces the partition is in usergroup wheel and has sudo acces. /etc/fstab looks like this: /dev/sda5 /media/winC ntfs defaults 0 2 /dev/sdb1 /media/winD ntfs defaults

  • Unable to share slideshows in iPhoto, share option all greyed out except for email and iCloud

    iPhoto version 9.6 OS X Yosemite 10.10.1 Completed a slideshow share 11/28 via iPhoto to Facebook Created a new slideshow to upload to fb msg users however all fields in Share were greyed out except for email. While working with Apple support and wai