Interface member variable by defaultly static ?

Hi,
The interface member variable by defaulty public,static,final.
But i have one doubt.
It is public because it has to access out side the interface.
It is final because the value never changes
Why it should be static ???
Thanks,
Narendra babu.B

Member variables are an implementation detail. Java does not allow multiple implementation inheritance. Java does allow multiple subtyping through interfaces. So interfaces can't have member variables.
Consider the diamond of death:
interface A
    int x;
interface B extends A
interface C extends A
interface D extends B, C
}See the problem now?

Similar Messages

  • Default initialisation of member variables and local variables

    I don't understand why member variables are initialized with default values by Java.
    Objects are initialized with "null" and primitives with "0", except boolean, which is initialized with "false".
    If these variables are used locally they are not initialized. The compiler requires them to be initialized by the programer, for example "String s = null".
    Why? What is the use of that difference?
    And why are arrays always initialized with default values, no matter if they are member variables or local variables? For example String[] s = new String[10]; s[0] to s[9] are initialized with "null", no matter if "s" is a local or member variable.
    Can someone please explain that strange difference, why it is used? To me it has no sense.

    Most of the time I have to initialize a local variable
    with "String s = null" in order to use it because
    otherwise the compile would complain. This is a cheap
    little trick, but I think everyone uses it.
    I wouldn't agree with "most of the time". The only cases where it is almost necessary to do that is when the variable should be initialized in a loop or a try-catch statement, and that doesn't happen too often.
    If local variables were initiliazed automatically without a warning it would be a Bad Thing: the compiler could tell when there is a possibility that a variable hasn't been assigned to and prevent manymanymany NullPointerExceptions on run time.
    And you didn't answer me why this principle is not
    used with arrays if it is so useful as you think.
    Possibly it is much more difficult to analyse the situation in the case of arrays; what if values are assigned to the elements in an order that depends on run time properties such as values returned from a random number generator.
    The more special rules one has to remember, the more
    likely one makes errors.I agree, but what is the rule to remember in this case?

  • Static member variable conundrum

    I'm using an abstract base class so that subclasses can share common functionality.
    Each subclass needs a static member variable (a String) that needs to be accessed from the abstract base class in some of it's methods.
    This is where the problem is; I would like to declare an abstract variable in the base class so that the base class methods can use the variable, but you can't make an abstract variable static.
    Is there a way around this or am I approaching this problem from the wrong direction?

    I can't remember if you can override static methodsTested this and determined that you can't. The superclass static gets called. Makes sense.
    I also tested my suggested code, and made a few improvements ... maybe it isn't quite so ugly after all (can you tell that I'm slogging through some boring stuff?):
    public class Tester1
        protected static HashMap _stringTable = new HashMap();
        static
            _stringTable.put(Tester1.class, "Tester 1");
        protected String getString()
            return (String)_stringTable.get(this.getClass());
        public static void main(String[] argv)
            Tester1 t1 = new Tester1();
            Tester2 t2 = new Tester2();
            Tester1 t3 = new Tester2();
            System.out.println("t1.getString() = " + t1.getString());
            System.out.println("t2.getString() = " + t2.getString());
            System.out.println("t3.getString() = " + t3.getString());
    public class Tester2 extends Tester1
        static
            _stringTable.put(Tester2.class, "Tester 2");

  • Interface with variables

    Hello all, I have a problem that has stumped even the smartest software engineer I know. Why does the following slice of code always print out "This does not work as expected". It seems to me that the 'int' inside of the interface is implecitly final, but the compiler should throw a compiler error if that were the case. When I use the debuger packaged with JBuilder 9 the value of the 'int' is 1 as it should be when it gets to the condition, but it still enters the conditions as if it were a 0. Thank you in advance for your help.
    public class instanceOfInterface {
    public static void main(String argc[]) {
    interfaceDef2 id = new interfaceDef2() {
    public int someInt = 1;
    if (id.someInt == 1) {
    System.out.println("This works properly.");
    else if (id.someInt == 0) {
    System.out.println("This does not work as expected.");
    interface interfaceDef2 {
    public int someInt = 0;
    }

    No. Try the example by defining a base class with a
    non static field, extending this class and referencing
    the field with a reference to the subclass. Then
    assign the subclass reference to a superclass variable
    and do the same. You will see the same difference you
    see in this example. E.g.:
    public class TestFieldOverride {
    public static void main(String[] args) {
    Bar b = new Bar();
    Foo f = new Foo();
    Bar bf = f;
    System.out.println("Bar i = " + b.i);
    System.out.println("Bar getI = " + b.getI());
    System.out.println("Foo i = " + f.i);
    System.out.println("Foo getI = " + f.getI());
    System.out.println("BarFoo i = " + bf.i);
    System.out.println("BarFoo getI = " +
    = " + bf.getI());
    System.exit(0);
    }Question, do you know why this is permitted? It seems to me that this behavior, if actually used in real code would quickly create a maintance nightmare. I don't think that you even need to setup a strawman argument of 'what if every class you create has an i variable to show that this could easily be very confusing to a programmer.
    My only guess is so that you can create a private version of a variable that has the same name as a parent classes variable in a subclass. Thus reducing the pressure to create unique and very long names for the programmer. But for public/protected/default access member variable names shouldn't be avaliable for subclasses to override. Course it would probably break code to change it now. So it probably won't happen.

  • SwingWorker and member variable thread safety

    If I have the following code:
    import javax.swing.SwingWorker;
    public class TestWorker extends SwingWorker {
         private String result = "Default";
         @Override
         protected Object doInBackground() throws Exception {
              if (Math.random() > 0.5) {
                   result = "Bigger";
              else {
                   result = "Smaller";
              return null;
         @Override
         protected void done() {
              System.out.println(result);
         public static void main(String[] args) {
              new TestWorker().execute();
    }Am I guaranteed to always get an output of either 'Bigger' or 'Smaller' or will I sometimes get 'Default'? The crux of the question is, will the background thread which updates the member variable 'result' be guaranteed to flush its cache by the time the done() method is called on the Event Dispatch Thread? I know I could guarantee the freshness of 'result' by making it volatile but I just wanted to know out of intellectual curiosity if SwingWorker tackles this.

    The SwingWorker object is still strongly reachable from other places even though it is no longer reachable from your main() method.
    In short, the work that is done on the EDT is a Runnable object that has a reference to the SwingWorker. So even through the SwingWorker may have finished running, there are still references to it so it cannot go completely out of scope (and thus, it's variables cannot go out of scope). The references to the SW are from the runnable, which is referenced from the EventQueue (i.e, EventQueue.invokeLater()), which is either static or referenced from the EDT.

  • How to reference a static variable before the static initializer runs

    I'm anything but new to Java. Nevertheless, one discovers something new ever' once n a while. (At least I think so; correct me if I'm wrong in this.)
    I've long thought it impossible to reference a static variable on a class without the class' static initializer running first. But I seem to have discovered a way:
    public class Foo  {
      public static final SumClass fooVar;  // by default initialized to null
      static  {
         fooVar = new SumClass();
    public class Bar  {
      public static final SumClass barVar;
      static  {
         barVar = Foo.fooVar;  // <<<--- set to null !
    }Warning: Speculation ahead.
    Normally the initial reference to Foo would cause Foo's class object to instantiate, initializing Foo's static variables, then running static{}. But apparently a static initializer cannot be triggered from within another static initializer. Can anyone confirm?
    How to fix/avoid: Obviously, one could avoid use of the static initializer. The illustration doesn't call for it.
    public class Foo  {
      public static final SumClass fooVar = new SumClass();  // either this ..
    public class Bar  {
      public static final SumClass barVar = Foo.fooVar;  // .. or this would prevent the problem
    }But there are times when you need to use it.
    So what's an elegant way to avoid the problem?

    DMF. wrote:
    jschell wrote:
    But there are times when you need to use it. I seriously doubt that.
    I would suppose that if one did "need" to use it it would only be once in ones entire professional career.Try an initializer that requires several statements. Josh Bloch illustrates one in an early chapter of Effective Java, IIRC.
    Another classic usage is for Singletons. You can make one look like a Monostate and avoid the annoying instance() invocation. Sure, it's not the only way, but it's a good one.
    What? You only encounter those once in a career? We must have very different careers. ;)
    So what's an elegant way to avoid the problem? Redesign. Not because it is elegant but rather to correct the error in the design.<pff> You have no idea what my design looks like; I just drew you a couple of stick figures.If it's dependent on such things as when a static initializer runs, it's poor. That's avoidable. Mentioning a case where such a dependency is used, that's irrelevant. It can be avoided. I know this is the point where you come up with a series of unfortunate coincidences that somehow dictate that you must use such a thing, but the very fact that you're pondering the problem with the design is a design problem. By definition.
    Besides, since what I was supposing to be a problem wasn't a problem, your "solution" isn't a solution. Is it?Well, you did ask the exact question "So what's an elegant way to avoid the problem?". If you didn't want it answered, you should have said so. I'm wondering if there could be any answer to that question that wouldn't cause you to respond in such a snippy manner. Your design is supposedly problematic, as evidenced by your question. I fail to see why the answer "re-design" is unacceptable. Maybe "change the way the Java runtime initializes classes" would have been better?
    This thread is bizarre. Why ask a question to which the only sane answer, you have already ruled out?

  • How to use member variable to all my pakages

    i want to use some member variables to all my packages in my project how to declare it.

    why doesn't declaring it public works for you. You may declare it public static, if want to get rid of creating objects of the containing class.

  • Accessing member variable

    Hello techies,
    Iam trying to access the member variables of an object which is added to arrayList.
    I had 2 arrayLists.
    Now i am converting it into Object[] using toArray()
    Now iam comparing these two arraylists.
    Now iam trying to access the memeber vairables of each object which is added to arraylist .
    But iam unable to access the member variable of each object.
    Object a[] = a1.toArray();
              Object b[] = a2.toArray();
              for (int i = 0; i < a.length; i++) {
                   System.out.println("inside for loop");
                   for (int j = 0; j < b.length; j++) {
    // a.
                        int o = compareObjects.compare(a[i], b[j]);
                        System.out.println("returned value is" + o);
    Here is my code::
    import java.util.*;
    import java.io.*;
    import java.lang.*;
    class exampleObject implements Comparator {
         String s = null;
         int i;
         int o;
         public exampleObject() {
         public exampleObject(int p, int q) {
              i = p;
              o = q;
         public int compare(Object o1, Object o2) {
              exampleObject k = (exampleObject) o1;
              exampleObject kk = (exampleObject) o2;
              // System.out.println("k.i"+k.i);
              // System.out.println("kk.i"+kk.i);
              if (k.i == kk.i) {
                   System.out.println("inside if condition" + k.i);
                   System.out.println("same i values");
                   if (k.o == kk.o) {
                        System.out.println("kk.o");
                   } else {
                        System.out.println("same p values");
                        return kk.o;
              return k.i;
    class ArrayListObjectCompare {
         // public static BufferedReader readFile;
         public static exampleObject ex;
         public static void main(String args[]) {
              ArrayList a1 = new ArrayList();
              ArrayList a2 = new ArrayList();
              ex = new exampleObject(2, 3);
              exampleObject ex1 = new exampleObject(3, 4);
              exampleObject ex2 = new exampleObject(4, 5);
              exampleObject ex3 = new exampleObject(5, 6);
              exampleObject ex4 = new exampleObject(6, 7);
              a1.add(ex);
              a1.add(ex2);
              a1.add(ex4);
              a2.add(ex1);
              a2.add(ex3);
              exampleObject compareObjects = new exampleObject();
              Object a[] = a1.toArray();
              Object b[] = a2.toArray();
              for (int i = 0; i < a.length; i++) {
                   System.out.println("inside for loop");
                   for (int j = 0; j < b.length; j++) {
    // a[i].k(-----here iam getting error);
                        int o = compareObjects.compare(a[i], b[j]);
                        System.out.println("returned value is" + o);
    can any tell me how to access the member variable of each object which is added to arrayList.
    I will be very thankful if any body replies or modifies my code.
    thanks(inadvance),
    ramu

    I'm not sure if they work 100% correctly yet after the forum "upgrade" last week, but please use code tags (see button above posting box) when posting code.
    You need to cast your Object back to exampleObject:
    exampleObject myObj = (exampleObject)a[0];
    System.out.println(myObj.i);Or, try using the other "toArray" overload:
    exampleObject a[] = a1.toArray(new exampleObject[a1.size()]);Then your "a" array elements are already of class exampleObject, and you don't need to cast each element individually.

  • Linker error 2005 when using member variable of type CNiGraph in CW++

    Hi,
    when I try to use a member variable of type CNiGraph I receive linker errors LNK2005, e.g.:
    msvcrtd.lib(MSVCRTD.dll) : error LNK2005: __CrtDbgReport already defined in libcmtd.lib(dbgrpt.obj)
    The project was created with the NI Measurement AppWizard.
    CW Version 3.0.1(549)
    OS: Windows 2000
    Thank you for your help.
    Uwe Gratzke

    This really isn't enough info about your project to give a definative answer, but one possibility is that you could have set up your project to be statically linking to the MFC libraries which we don't support. It could be a lot of other possibilities also. If you would like, you could have our support engineers help you with the project by sending it to us via http://www.ni.com/ask.
    Best Regards,
    Chris Matthews
    Measurment Studio Support Manager

  • Can't see member variables when debugging with jrockit

    Hi all,
    I'm using WLS 8.1.4 with jrockit and trying to do remote debugging while connecting from IntelliJ Idea 4.5.4. Everything works fine except that I can't see a class's member (or instance) variables in the debugger. I can see static and local variables, step through the code, etc. If I switch to Sun's jvm that came with WLS (1.4.2 05) I can see member variables. Is there a setting to change this or is it a known issue? I'm using -Xdebug -Xrunjdwp:transport=dt_socket,server=y,address=5005,suspend=n as my startup args.
    thanks

    I just check with IntelliJ support on this issue. They say that its a known issue with IntelliJ 4.5. According the them a fix has been implemented and debugging of member variables is possible in IDEA 5.x versions.

  • Making  member variable as serializable

    Hello Techies,
    can we make a perticular member variable of a object as serializable.
    let us suppose we are having one class
         class a
                   int i =0;
                  String s = "ramu";
                  public void show()
                          s.o.p("this is xxx");
       class b
             public static void main(String args[])
                       a   x = new ();
       }can I make the variable i as serializable. if so how can it be??
    regards,
    ramu.

    What do you mean? You can make classes serializable, not member variables.
    There's the keyword "transient" (look it up in your favourite Java book) which will prevent a member variable of a class from being serialized. So if in your class a you only want the value of i (and not the value of s) to be serialized when you serialize an instance of class a, you should make s transient.

  • Member variable for jsp tag

    Hi,
    is it safe to have member variables in jsp tag? every time service method will create new instance of tag?Thanks

    is it safe to have member variables in jsp tag? The answer is yes, but.
    every time service method will create new instance of tag?no, it will not necessarily create a new instance every time.
    For most tags I believe the spec allows for a 'pool' of tag handlers to be retained.
    Certainly I have seen this happen in tomcat.
    An exception is the JSP2.0 SimpleTagHandler which guaruntees that it will be used once and then thrown away.
    With other tags, you have ownership of the tag and its member variables, between doStartTag and doEndTag. Nothing else can interfere with the member variables.
    However if you include the tag twice in your page, one after another, it is quite possible, that you will be given the same tag object, and thus the member variables might have variables set from the previous use of the tag.
    It is fairly easy to work around though
    in the doStart: init any member variables that are not defined by attributes to be their default values
    in the doEnd method, clean up your member variables. Clear any attribute values that might be optional. Set things to null. That way you are guarunteed that if the tag is reused, it won't pick up any data from your run through here.
    But don't take my word for it :-) Try it out.
    Hope this helps,
    evnafets

  • I have a HP OfficeJet Pro K550 with a default static IP address and I can't access it to change it.

    Because this printer's default static IP is not in my subnet and my network does not have a DHCP server I am unable to access the settings I need to change the IP address to one I can use.  I am using Windows 7 64-bit and am plugged into the printer via USB.  I know if I had an IP address from my network I could type it into the address bar and open up far more settings but I am unsure how to do that since the default IP is not on my network.
    This question was solved.
    View Solution.

    I assume you cannot log into the printer's subnet temporarily?  Can you disconnect the printer from that subnet temporarily?  Can you connect an Ethernet cable between the printer and your PC?
    Say thanks by clicking "Kudos" "thumbs up" in the post that helped you.
    I am employed by HP

  • GetVariable() doesn't work in FireFox for object member variables

    I'm trying to access Flash member variables from Javascript
    using GetVariable. The following line of code works for IE but not
    for Firefox:
    var variable = swf.GetVariable("someObject.someProperty");
    In IE, the GetVariable() function returns the correct value
    for the property. However in Firefox, the GetVariable() function
    returns "null" when trying to access the property. If I just try to
    access the object in Firefox (i.e. swf.GetVariable("someObject"), I
    get a string back (instead of the object that I am expecting). If I
    try to access a variable (such as a string) and not an object using
    GetVariable(), this works in both Firefox and IE.
    Any ideas what I am doing wrong? I suspect that my syntax for
    accessing object member variables in Firefox is incorrect but I
    have not been able to find any documentation on the correct syntax.
    Any assistance would be greatly appreciated.
    Thanks.

    Thanks, but this isn't an issue.
    The window.document.flashMovie will return the correct flash
    object/embed flash movie.
    The issue is that the GetVariable method wont return the
    object variable. When you try to get an simple variable that is in
    the _root timeline, it will return the value. Example:
    movie.GetVariable(_url) will return the url of the flash
    movie without any problem.
    But when you create an ActionScript class with
    userName property and then you will create the instance of
    it in the _root, you should be able to get it's value. Example:
    Flash:
    var obj = new SomeClass();
    obj.userName = "peter";
    JavaScript:
    movie.GetVariable("/obj:userName");
    or
    movie.GetVariable("obj.userName");
    This will return
    Peter for IE and null for Forefox/Opera.
    The same when you use the document.embeds[..].

  • Class/member variables usage in servlets and/or helper classes

    I just started on a new dev team and I saw in some of their code where the HttpSession is stored as a class/member variable of a servlet helper class, and I was not sure if this was ok to do or not? Will there be problems when multiple users are accessing the same code?
    To give some more detail, we are using WebLogic and using their Controller (.jpf) files as our servlet/action. Several helper files were created for the Controller file. In the Controller, the helper file (MyHelper.java) is instantiated, and then has a method invoked on it. One of the parameters to the method of the helper class is the HttpServletRequest object. In the method of the helper file, the very first line gets the session from the request object and assigns it to a class variable. Is this ok? If so, would it be better to pass in the instance of the HttpServletRequest object as a parameter to the constructor, which would set the class variable, or does it even matter? The class variable holding the session is used in several other methods, which are all invoked from the method that was invoked from the Controller.
    In the Controller file:
    MyHelper help = new MyHelper();
    help.doIt(request);MyHelper.java
    public class MyHelper {
        private HttpSession session;
        public void doIt(HttpServletRequest request) {
            session = request.getSession();
            String temp = test();
        private String test() {
            String s = session.getAttribute("test");
            return s; 
    }In the past when I have coded servlets, I just passed the request and/or session around to the other methods or classes that may have needed it. However, maybe I did not need to do that. I want to know if what is being done above will have any issues with the data getting "crossed" between users or anything of that sort.
    If anyone has any thoughts/comments/ideas about this I would greatly appreciate it.

    No thoughts from anyone?

Maybe you are looking for

  • Help only have 14 gb left of 500 gb on MacBook Pro. Can I upgrade to 1Tb?

    Help only have 14 gb left of 500 gb on MacBook Pro. Can I upgrade to 1Tb? (I purchased it August 2010) If not any suggestions to increase storage. Majority of my 500 gb is Desktop (assuming that is mainly snow leopard?) pictures and music. Don't real

  • Dms server

    hi everyone we are using ECC 6.0, win 2003, oracle 10g. what is document management server? how to connect to market place from sap through level,what are the parameters to be changed? how to set up a icon on usert desktop's so that by clicking on th

  • GR based IV Indicator do not appear in PO for Raw material

    Dear All, I am creating a PO for material with cost center . Vendor is maintained with GR based IV and ERS Indicators. If a non valuated material (i.e. with quantity update and without value update)  is used in PO , I am able to  see  Tick for GR bas

  • Importing a DV File

    I have a DV file on my hard drive that I want to import into an iMovie project. I've never really used iMovie before. Really all I want to do is insert chapter markers for use in iDVD. The file's 18.88 GB. When I drag the file into iMovie, the "impor

  • "Can't connect to iPad Software Update Server" Error. How Can I Fix This?

    I keep getting an error saying, "The iPad cannot be updated at this time because the iPad software update server could not be contacted or in temporarily unavailable.", whenever I try to update to 3.2.2 or restore my iPad. This has been happening for