Comparison between constants in class and interface

Hi,
I have recently read an article which says it is a better use of your memory if you declare constants in an Interface rather than in a Class. Can any one explain this is more detail? Have a great day.
Thank you,
npaila

@ricardo_moral
Hm ...
we are talking of constants in this topic - constants are always final, otherwise it would not be garanteed, that they cannot change. Also we are not talking of holding a copy of this constant field in a class, we are talking of directly referring to this static final field, regardless if it is declared in an interface or in a class.
I prefer using an interface - my reason is that I can use it the same way, I can use a class, which declares this constant - via qualifier - AND I can implement it, if I want to - so, I follow the swing route and declare constants, which are shared by many classes, in an interface - but I implement it only there, where these constants are heavily used.
greetings Marsian

Similar Messages

  • What is the difference between Abstract class and Interface ?

    Hi,
    Could u plz tell me the difference between Abstract class and Interface?
    Thanks in advance.
    Gopi

    Lots.
    An abstract class can contain some method implementations, or indeed all the method implementations. It may contain methods with all the various access modifiers. It cannot be instantiated. A class may inherit from only a single abstract class.
    An interface contains only public method stubs and constants. A class may implement multiple interfaces. An interface cannot (obviously) be instantiated.
    Abstract classes are particularly useful when you need to provide a semi-complete implementation for reuse. Interfaces are used more like types.
    Look at java.util.* for some good examples of the use of both.

  • Difference between Abstarct Class and Interface

    HI,
    Here is a simple one for the gurus but quite important for me.
    What is the difference b/w an Interface and an abstract class?
    Why do we need each one of them?
    I would appreciate if you people can give examples of each so that I amy understand fully
    Thanks in advance...

    A normal class (not abstract) has a special behaviour, like java.awt.Frame. A Frame is a frame no matter how you subclass it. If you create a subclass it will still be able to be displayed by calling it's show method. So by using a normal class you can create a type of class.
    An abstract class does the same, but it leaves some of the code unwritten. For instance java.lang.Number. This class is abstract becuase it has no knowledge of how to store the number in memory. But it knows that it is a number, and any subclass will still be a number. You could create a subclass of Number that can hold the time in milliseconds and checks your system clock to see what the time is, it could also have methods that return the time in another country, but it would still be just a Number.
    An interface is a way to describe what an object can do, not what it is. So with java.lang.Comparable as an example you can make any class comparable. This means that no matter what type of object you have, it can be compared with other objects. So you can have a subclass of Frame that can be compared with other windows. Or a subclass of Number that can be compared with other Numbers. You can even compare those two different types if you like. So you could compare a window with a number.
    That is the difference between abstract classes and interfaces.
    I hope you could follow my arguments, it isn't an easy subject,
    Daniel

  • Difference between abstract classes and interfaces

    I actually wonder about what are the differences between abstract classes and interfaces may somebody give an example code about it?
    and i have one more question how can i use interfaces like multiple inheritance ? i mean when i implement an interface like
    class a extends b implements c,di have to use all c and d methods but what that methods means?
    I mean as i know we cannot make implementations of methods in interfaces
    but for example in runnable interface there is a method like run() and it has been defined somewhere because it knows what to do(i mean when it will run), i just write my code into that method .

    Once you get past the starting point (I am referring to the OP here), there are a few salient differences:
    You can only extend (or generalize) a single superclass; however, you can implement (or realize) multiple interfaces. As such, all things being equal, using an interface in lieu of an abstract class 'frees' your design. Later, if you want the implementor of an interface to inherit from another class, there is not issue.
    Any abstract method specifies a contract. However, abstract classes allow you to also add common behavior to subclasses. This is an overused justification for abstract classes, IMO. You can achieve the same effect using delegation and still having interfaces.
    Always program to interfaces wherever possible. This means that you define an interface and have an implementing class (usually at a minimum). Do not do this for all your classes, but rather the ones that make your system unique (the domain model or M in MVC architecture). This allows you to later change implementation with a minimal amount of refactoring. This is a core precept from the Group of Four and any number of decent programming books.Best of luck.
    - Saish

  • Difference between abstact class and interface

    hi
    i am new in java.
    plz tell me abstract class support which type of polymorphisam and
    interface support which type of polymorphisam
    plz explain
    bye
    siva

    There's only one type of polymorphism.
    Does that make it mono-polymorphism? ;)
    Indeed - do a search for the forums.
    The short answer is that both abstract classes and interfaces define required method signatures for subclasses that extend/implement them, but abstract classes can have implementation for some or all methods that are not abstract; interfaces have no implementation whatsoever for any methods.

  • Abstract class and interface????

    hi all, i would like to know the difference between abstract class and interface....could anybody enlighten me on this....

    Like the others have mentioned you can implement more than one interface, but only inherit from one abstract class. An abstract class can provide some implementation.
    steve http:\\www.jamonapi.com

  • Compiling Nested Classes and Interfaces

    I am looking for documentation about compiling nested classes and interfaces. I have found something in the JVM Specification, but there does not explain how a nested class is compiled and what is included in the top level class to mark a "place holder" to the nested class. The JVM Specification in this topic cite the web page http://java.sun.com/products/jdk/1.1/docs/guide/innerclasses/spec/innerclasses.doc.html that does not exists any more.
    My root problem is that: I am compiling a class with a private nested class, but in the class file generated TopLevelClass$NestedClass.class the class does not have the private modifier. So I am not understanding why the "private" modifier was removed during compilation.
    I performed the same test with a protected nested class and the result was the nested class with the public modifier. So I am not understanding why the "protected" modifier was changed to "public".
    Thanks in advance,
    Mardoqueu.

    This should not be happening. What compiler are you using? If it's a reasonably recent Sun compiler, could you post a minimal example?

  • Documentation for Oracle XML parser Classes and Interfaces

    I would like to use the oracle XMLParser V2 classes with JDeveloper 3.1, but I cannot find any reference documentation for the classes and interfaces in help.
    I am especially interested in a documentation for the XSLProcessor class. The only information I found was provided with the few provided samples.

    I don't think the javadoc for this was included in the release. You can get the Javadoc (as well as the latest version of the XML Parser v2) here on OTN.
    Take Care,
    Rob
    null

  • About Classes and Interfaces

    How can we define the classes and interfaces in an interview can u give me any realtime example for this

    How can we define the classes and interfaces in an
    interviewThe easiest explanation probably is to say that an interface in Java is a special class, namely a totally abstract one, that has been given a separate name.
    Classes and interfaces (because they're classes in disguise) both constitute types, meaning that variables can be declared of them. The difference is that a class can carry implementation whereas an interface cannot (because it's totally abstract).
    Java has single inheritance of implementation which means the the inheritance of classes is restricted to exactly one while you can inherit as many interfaces you like.

  • What are the classes and interface in JDBC

    Please anybody tell me
    What are the Classes in JDBC?
    and
    What are the Interfaces in JDBC?
    Will you please show the classes in one table and interfaces in one table. Plesae
    regards
    pooja.

    Hi jeyan,
    Very more thanks. I am searching lot of time in net to exactly get the classes and interface in jdbc. Now you have given helpful link.
    thank you.
    regards
    pooja

  • Question about Classes, Abstract  Classes and Interfaces.

    I have been experimenting with Classes, Abstract Classes and Interfaces and wonder if anyone can explain this to me.
    I was looking for a way to assign a value to a variable and then keep it fixed for the session and have devised this.
    First I create an abstract class like this:
    public abstract class DatabaseConnection {
    private static String ServerName = null;
    public static void setServerName(String serverName) {
              ServerName = serverName;
         public static String getServerName() {
              return ServerName;
    }and then I created an interface
    public interface DatabaseAccess {
         String servername = DatabaseConnection.getServerName();
    }And finally the class itself with some test lines in it so I could see what was going on:
    public class CreateDatabase extends DatabaseConnection implements DatabaseAccess {
         public static void main (String args[]){
              new CreateDatabase();
         public CreateDatabase(){     
              setServerName("Server Name 1");
              System.out.println ("Before update ");
              System.out.println ("ServerName from Interface           = " + servername);
              System.out.println ("ServerName from Abstract Class = " + getServerName());
              System.out.println ("After update ");
              setServerName("Server Name 2");
              System.out.println ("ServerName from Interface           = " + servername);
              System.out.println ("ServerName from Abstract Class = " + getServerName());
              System.out.println ("==========================");
    }The output I get from the above is:
    Before update
    ServerName from Interface           = Server Name 1
    ServerName from Abstract Class = Server Name 1
    After update
    ServerName from Interface           = Server Name 1
    ServerName from Abstract Class = Server Name 2
    ==========================I also tried this in another class which calls the above class to see if I get the same effect
    public class CheckDatabaseAccess {
         public static void main (String args[]){
              new CreateDatabase();
              CreateDatabase.setServerName("Server 3");
              System.out.println("CreateDatabase "+CreateDatabase.servername);
              CreateDatabase.setServerName("Server 4");
              System.out.println("CreateDatabase "+CreateDatabase.servername);
              CreateDatabase.setServerName("Server 5");
              System.out.println("CreateDatabase "+CreateDatabase.servername);
    }The output of which is this:
    Before update
    ServerName from Interface           = Server Name 1
    ServerName from Abstract Class = Server Name 1
    After update
    ServerName from Interface           = Server Name 1
    ServerName from Abstract Class = Server Name 2
    ==========================
    CreateDatabase Server Name 1
    CreateDatabase Server Name 1
    CreateDatabase Server Name 1Can anyone explain why I appear to only be able to change or set the ServerName only the once?
    Is this the correct way to do it? If it is it's exactly what I am looking for, a way to set the value of variable once in a session and then prevent it being changed.
    Or is there a better way of doing this.
    What I want to use this for is for example, storing the accesses to a database on a server. I won't know what server the database will be stored on nor what the database is called so I create an INI file which stores this information in encrypted format, which is set by the database administrator. It occurs to me I can use this method to then retrieve that data once and once only from the INI file and use that throughout the life of the session to access the database.
    Any help appreciated
    Regards
    John

    Not gonna read all of it, but this jumps out:
    public abstract class DatabaseConnection {
    private static String ServerName = null;
    public interface DatabaseAccess {
         String servername = DatabaseConnection.getServerName();
    }You have two completely separate variables (with two different names, for that matter, since you were inconsistent in your capitalization, but it wouldn't make a difference if they did have the same name with the same case). And the one in the interface is implicitly public, static, and final.
    Anytime you refer to "servername" through a reference of type DatabaseAccess, it refers to the one declared in the interface.
    Anytime you refer to "ServerName" inside the DatabaseConnection class, it refers to the one declared in that class.

  • What happens as we use strictfp modifier with class and interface ?

    I just knew that strictfp can't be used with variables?
    I want to know that why we use strictfp with class and interface?

    makpandian wrote:
    i hope i can understand if you explain myself.Okay, let me go ahead and try to explain yourself.
    The concept of a makpandian can be summed up as the following: a simple-minded, java forum-goer who posts questions directly from his SCJP 6 study guide in hopes that forum members will give him a simple answer that he can correlate to a multiple-choice response. By doing this, the makpandian suspects that passing the SCJP 6 exam will become a tangible reality, thus enabling it to obtain a job in the public work sector (given the great significance of this type of certification in the makpandian's presumed country of origin (India, for those of you who aren't following along)).
    So how'd I do? Did I do a good job of explaining yourself?

  • Difference between a static class and singleton

    What is the difference between a static object and a singleton ?
    for example:
    1 )
    public class MyObject {
    static MyObject singleton ;
    public static MyObject getInstance() {
    if (singleton ==null) singleton = new MyObject ();
    return singleton;
    private MyObject () {
    supe();
    public void myMethod() {
    2 )
    public class MyObject {
    private MyObject () {
    supe();
    public static void myMethod() {
    If I need to call the myMethod() , witch of this solution is better ?
    // case 1
    MyObject.getInstance().myMethod()
    // case 2
    MyObject.myMethod()
    .....

    This has been discussed a lot here.
    Use the forum's search feature, or, since it's not necessarily reliable, use google.
    For many intents and purposes, they're equivalent.
    However, with a singleton you can implement an interface, and get polymorphic behavior. You can't do with with a class full of static methods (which, by the way, is NOT a static class).

  • [AS-CS5] Comparison between text style range and character style

    Hi,
    It's been a while since I'm not coming on this forum ...
    I'm looking to make a comparison between a "text style range" and a character style.
    I have no problems, except for the comparison of "fonts".
    myCurrentText = text for char…
    theStyle = character style id…
    I try:
           if (applied font of myCurrentText ≠ applied font of theStyle) then set theCharDifference to 1 
    The selected text give me:
           font "Minion Pro        Italic" of application "Adobe InDesign CS5.5"
    and the character style:
           "Minion Pro"
    I try "set theTextFont to font of applied font of myCurrentText" -> Error
    This is my script (it's too long to post here) <http://db.tt/BLoGPiN>.
    Loot at the "on CheckCharacterDifference(myCurrentText, theStyle)"
    TIA.
    Oli.

    I've written this, but I do not know if it covers all cases ...
    Someone?
    Thanks.
    Oli.
    set theText to item 1 of selection
    set theStyleChar to properties of character style 2
    -- analysis of the properties of the text
    set myTextFontApplied to applied font of theText
    set myTextFontFamily to font family of applied font of theText
    set myTextFontStyle to font style of theText
    -- analysis of the character style
    set myStyleCharFontStyle to applied font of theStyleChar
    if myStyleCharFontStyle = "" then
         set myStyleCharFontStyle to font style of theStyleChar
         if myStyleCharFontStyle ≠ myTextFontStyle then set theDifference to true
    else
         if myStyleCharFontStyle ≠ myTextFontFamily then set theDifference to true
    end if
    Message was edited by: OlivierBerquin

  • Abstract class and interface having same method

    Hello,
    Here is my problem. Suppose we have one abstarct class and one interface.Here is code-
    //Abstarct class
    abstract class X{
    abstract void myMethod();
    //Interface
    public interface Y{
    abstract void myMethod(){}
    Now i have a class which extends both abstarct class X and interface Y.
    If i call myMethod() from this class. Whose myMethod would be called.Will it be of abstract class or interface?
    Many Thanks

    Hello,
    Here is my problem. Suppose we have one abstarct class
    and one interface.Here is code-
    //Abstarct class
    abstract class X{
    abstract void myMethod();
    }OK, so far...
    //Interface
    public interface Y{
    abstract void myMethod(){}
    }An interface cannot have code (the {} part), so this won't work.
    Lets pretend though, it read
    //Interface
    public interface Y{
    abstract void myMethod();
    However, the abstract class above can have code;
    If you extended X and implemented Y (with no code in it), you would have to have a myMethod() implementation in your code. That's the one that would run.
    Now, let's pretend the abstract class above did have code in it.
    //Abstract class
    abstract class X {
    abstract void myMethod() { System.out.println("Hello"); }
    Then, you wouldn't have to have a myMethod() implementation in your class which extends X and implements Y (it's defined in X). If you didn't have one, the method in X would run. If you defined your own myMethod() implementation in your class (which extends X and implements Y), then your own implementation would run.

Maybe you are looking for