Why is everything static?

Hey
I'm trying to split some code from 1 class into a couple other classes.. not adding any new functions just moving it around so my classes have a better overview
now while trying to do that I moved some code.. figured I don't have the same buttons and ints and stuff so I moved some of those which I don't need in the other class anymore
some I need everywhere though.. so I figured I should make them public instead of private.. but now I can't use them because "Cannot make a static reference to the non-static field Main.rechtsonder " while the button isn't declared as static..
and I have that for quite a few things.. so I was wondering what the hell is static and why are those things static?

A static field or a static method is a member of a class that still the same for all the instances of that class.
Try to compile and run the following code:
public class Example {
     private String privateStringField;
     private Integer privateIntegerField;
     private static String sharedField ="SHARED";
     public Example(String privateStringField, Integer privateIntegerField) {
          super();
          this.privateStringField = privateStringField;
          this.privateIntegerField = privateIntegerField;
     public String toString() {
          StringBuffer toreturn = new StringBuffer("privateStringField: "+privateStringField);
          toreturn.append("\n");
          toreturn.append("privateIntegerField: " + privateIntegerField);
          toreturn.append("\n");
          toreturn.append("sharedField: " + sharedField);          
          return toreturn.toString();
     public static String getSharedField() {
          return sharedField;
     public Integer getPrivateIntegerField() {
          return privateIntegerField;
     public void setPrivateIntegerField(Integer privateIntegerField) {
          this.privateIntegerField = privateIntegerField;
     public String getPrivateStringField() {
          return privateStringField;
     public void setPrivateStringField(String privateStringField) {
          this.privateStringField = privateStringField;
     public static void setSharedField(String sharedField) {
          Example.sharedField = sharedField;
     public static void main(String[] args) {
          Example one = new Example("one",new Integer(1));
          Example two = new Example("two",new Integer(2));
          System.out.println(one);
          System.out.println(two);
          System.out.println("");
          one.setPrivateIntegerField(new Integer(100));
          one.setSharedField("SHARED modified by one");
          System.out.println(one);
          System.out.println(two);
          System.out.println("");
          System.exit(0);
}

Similar Messages

  • Help! Why did everything disappear?

    I was changing the fill on a shape I drew on a new layer, when suddenly everything I had done before disappeared and I now have only one layer with one frame.  What happened??  And can I get everything back?  I had a layer with a static photo, a layer with a symbol and simple motion tween (swoop in from left), and a third layer I was working on.  There were 36 frames in the file.  Now, Layer 1 (with one frame) has the photo and the shape showing.  I've been all over the forums and support looking for a clue.  Can anyone help me?  Thanks!!

    Hi Matt!
    Thank you so much for replying to my question ‹ I¹ll be able to go back to
    my Flash project later this evening, and I¹ll be able to answer these
    questions.  Is there some reason that two or three layers would disappear
    and only Layer 1 would remain?  Is there a ³flatten² type command like in
    Photoshop?  Thanks again!
    Ruth
    From: Matt Schneck <[email protected]>
    Reply-To: <[email protected]>
    Date: Tue, 11 May 2010 14:19:46 -0600
    To: Go for Joy <[email protected]>
    Subject: Help! Why did everything disappear?
    Were you working on the tween inside of an object?  It sounds like you have
    an object that has 36 frame that's sitting on frame 1 of the timeline.  Whe
    n you were manipulating your tween, or shape, or whatever on layer 3, you
    clicked out of your object and are just looking at your main timeline.
    If you deleted the graphic object, you may have deleted the work you did...
    Can you name the files in your library, the type of object they are, and a
    description of what each object contains?

  • Why is the static method in the superclass more specific?

    Why is the static method in the superclass more specific than the static method in the subclass? After all, int is a subtype of long, but Base is not a subtype of Sub.
    class Base {
        static void m(int i){ System.out.println("Base"); }
    class Sub extends Base {
        static void m(long l){ System.out.println("Sub"); }
    class Test {
        public static void main(String[] args) {
            int i = 10;
            Sub sub = new Sub();
            sub.m(i);
    }The first example compiles without error.
    Output: Base
    class Base {
        void m(int i){ System.out.println("Base"); }
    class Sub extends Base {
        void m(long l){ System.out.println("Sub"); }
    class Test {
        public static void main(String[] args) {
            int i = 10;
            Sub sub = new Sub();
            sub.m(i);
    }In the second example, both instance methods are applicable and accessible (JLS 15.12.2.1), but neither is more specific (JLS 12.2.2), so we get a compiler error as expected.
    : reference to m is ambiguous,
    both method m(int) in Base and method m(long) in Sub match
    sub.m(i);
    ^
    1 error
    Why don�t we get a compiler error for the static methods?

    Thank you for your ideas.
    ====
    OUNOS:
    I don't get Sylvia's response. This is about static methods, what are instances are needed for??Yes, the question is about static methods. I included the example with non-static methods for a comparison. According to JLS 15.12.2, both examples should cause a compiler error.
    And why you create a Sub object to call the method, and dont just call "Sub.m(..)"Yes, it would make more sense to call Sub.m(i). Let�s change it. Now, I ask the same question. Why is there no compiler error?
    ====
    DANPERKINS:
    The error in your logic stems from calling static methods on instances, as ounos pointed out. Solution: don't. You won't see any more ambiguities.A static member of a class may also be accessed via a reference to an object of that class. It is not an error. (The value of the reference can even be null.)
    Originally I was looking only at the case with non-static methods. Therefore, I used sub.m(i). Once I understood that case, I added the static modifiers. When posting my question, I wish I had also changed sub.m to Sub.m. Either way, according to JLS 15.12.2, a compiler error should occur due to ambiguous method invocation.
    ====
    SILVIAE:
    The question was not about finding an alternative approach that doesn't throw up an ambiguity. The question related to why, in the particular situations described, the ambiguity arises in only one of them.
    Yes.
    Proposing an alternative approach doesn't address the question.
    Yes.
    ====
    If anyone is really interested, here is some background to the question. Some people studying for a Sun Java certificate were investigating some subtleties of method invocations:
    http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=24&t=019182
    I remember seeing the non-static case discussed in this forum once before in a more practical context. jschell probably knows the link.

  • Why is everything so difficult since 'upgrading' to Maverick. I can't sync my Outlook calendar (default) with iCal or my iPad.

    Why is everything so difficult since 'upgrading' to Maverick. I can't sync my Outlook calendar (default) with iCal or my iPad.
    Also, I can't send pictures via Dropbox any more. Two basic necessities that I just can't get to work.

    Microsoft didn't update Outlook to be Mavericks ready.
    I don't really use Dropbox, but if their software doesn't work like it used to, they didn't update it for Mavericks.
    EDIT: http://www.iclarified.com/34312/dropbox-24-released-for-mac-os-x-brings-maverick s-support-iphoto-importer-more

  • Why is everything in my playback monitor blown up (by 200% or so?), and why is it showing as 4:3?

    Why is everything in my playback monitor blown up (by 200% or so?), and why is it showing as 4:3? I imported mxf files that show up fine in the preview monitor, but when edited into the timeline they now suddenly appear super blown up; there is a guy in a mid-shot interview situation, but now I only see his arm. Perhaps I hit  a wrong button by accident, but how can I resolve this? ALso, whether it's the same issue or whether it is unrelated I don't know, but in the playback monitor the aspect ratio seems 4:3; why is that? Any idea, on either topic, anyone? Thank you!!!!! I appreciate your help!!

    Did you do as it shows in http://forums.adobe.com/message/3776153 to be sure your video and sequence match?

  • At&t to T-Mobile on unlocked Bold - Why does everything work? Will it keep on working?

    Due to at&t's lack of coverage at my house, I changed to T-Mobile.  I unlocked my Bold and simply put the T-Mobile sim card in.
    To my surprise, EVERYTHING works perfectly.   I am on the BES Data plan.
    The one thing that I don't understand is why my BIS email accounts are still working.  I still manage these BIS accounts at att.blackberry.com even though I'm on T-Mobile's BES service now.  Shouldn't I be managing these from tmobile.com/bis or something like that?  If I just leave it alone, will it keep working indefinetely?
    Thanks

    I have the same problem. Developed just this past week. But my phone is older (iOS 3.1.3).  There are many replies in many forums here, but no answers. Does anybody have a solution? Could the headset jack or the MONSTER adapter for my jack have damaged the silencing function in the jack port?

  • Why is everything pixelated and blurry in CS6?

    Hi,
    A few months ago I installed the trial versions of Adobe Indesign, Illustrator and Photoshop on my MacBookPro.  After they expired I purchased the standard student edition and used the provided key number to unlock the full versions.  However, everything (including the sample fonts list, icons and opening menu) are pixelated and blurry.  As a design student, it is incredibly distracting to see font that isn't smooth like it should be on a retina display...
    I've updated Indesign to version 8.0.1 but to no avail.  What could be wrong and how might I solve this problem?
    Thank you,
    Cuong

    @Tereyn "Since the originator has not stated that the software was tested on a non-retina display how can he/she or you be sure the display is the origin of the problem?"
    Yes he does say that he is using a retina display.. Did you even read the post.
    "As a design student, it is incredibly distracting to see font that isn't smooth like it should be on a retina display..."
    If you have another problem why don't you create your own post?
    It's still annoying for me that there isn't a retina fix yet and it's been a few years now. It's just a few menu icons. Fix the retina issue please!

  • Problems with static member variables WAS: Why is the static initializer instantiating my class?!

    Hi,
    I have been hunting down a NullPointerException for half a day to come to
    the following conclusion.
    My constructor calls a method which uses static variables. Since an intance
    of my class is created in the static block when the class is loaded, those
    statics are probably not fully initialized yet and the constructor called
    from the static block has those null pointer problems.
    I've considered moving the initialization of the static variables from the
    declaration to the static block. But your code is inserted BEFORE any other
    code. Therefore not solving my problem.
    Two questions:
    1) what would be a solution to my problem? How can I make sure my static
    variables are initialized before the enhancer generated code in the static
    block calls my constructor? Short of decompiling, changing the code and
    recompiling.
    2) Why is the enhancing code inserted at the beginning of the static block
    and not at the end? The enhancements would be more transparent that way if
    the static variables are initialized in the static block.
    Thanks,
    Eric

    Hi Eric,
    JDO calls the no-args constructor. Your application should regard this constructor as belonging
    primarily to JDO. For example, you would not want to initialize persistent fields to nondefault
    values since that effort is wasted by JDO's later initilization to persistent values. Typically all
    you want to initialize in the no-args constructor are the transactional and unmanaged fields. This
    rule means that you need initialization after construction if your application uses the no-args
    constructor and wants persistent fields initialized. On the other hand, if your application really
    uses constructors with arguments, and you're initializing persistent fields in the no-args
    constructor either unintentionally through field initializers or intentionally as a matter of
    consistency, you will find treating the no-args constructor differently helpful.
    On the other hand, if Kodo puts its static initializer code first as you report, then it is a bug.
    Spec Section 20.16: "The generated static initialization code is placed after any user-defined
    static initialization code."
    David Ezzio
    Eric Borremans wrote:
    >
    Hi,
    I have been hunting down a NullPointerException for half a day to come to
    the following conclusion.
    My constructor calls a method which uses static variables. Since an intance
    of my class is created in the static block when the class is loaded, those
    statics are probably not fully initialized yet and the constructor called
    from the static block has those null pointer problems.
    I've considered moving the initialization of the static variables from the
    declaration to the static block. But your code is inserted BEFORE any other
    code. Therefore not solving my problem.
    Two questions:
    1) what would be a solution to my problem? How can I make sure my static
    variables are initialized before the enhancer generated code in the static
    block calls my constructor? Short of decompiling, changing the code and
    recompiling.
    2) Why is the enhancing code inserted at the beginning of the static block
    and not at the end? The enhancements would be more transparent that way if
    the static variables are initialized in the static block.
    Thanks,
    Eric

  • Why a non static member class can be defined in an interface

    Non-static member classes are defined as instance members of other classes, just like fields and instance methods are defined in a class. An instance of a non-static member class always has an enclosing instance associated with it.
    An interface can't be instantiated then how a non static member class will have an enclosing instance associated with it.
    interface outer
            public  class inner{
            public void p()
                System.out.println("inside interface's non static member class");
        public  static class inner1{
                public void p(){System.out.println("inside interface's  static member class");
    public class Client {                                           // (11)
        public static void main(String[] args) {                    // (12)
        outer.inner nonStatic = new outer.inner();
            nonStatic.p();
        outer.inner1 stat = new outer.inner1();
          stat.p();
    }inner is a non static member class even then " outer.inner nonStatic = new outer.inner();" working fine ?????????????

    class outer
            public  class inner{
            public void p()
                System.out.println("inside interface's non static member class");
    public class Client {                                           // (11)
        public static void main(String[] args) {                    // (12)
        outer.inner nonStatic = new outer.inner();
        nonStatic.p();
    }on compiling the above code the error message i got is
    "not an enclosing class: outer"
    the reason of this compilation error is "outer.inner nonStatic = new outer.inner();
    it should be "outer.inner nonStatic = new outer(). new inner();"
    now my question is
    interface outer
            public  class inner{
            public void p()
                System.out.println("inside interface's non static member class");
    public class Client {                                           // (11)
        public static void main(String[] args) {                    // (12)
        outer.inner nonStatic = new outer.inner();
        nonStatic.p();
    }on compiling the above code why compilation error is not coming??????????
    i think now it is more clear what i am asking

  • Why is the static initializer instantiating my class?!

    It seems that the static { ... } blocks for my enhanced classes actually
    create instances of those classes. Gack! It seems, furthermore, that
    this is due to the JDO spec itself, whose JDOImplHelper.registerClass()
    requires an instance of the class being registered. Double gack!
    This is causing a problem for me, because I've put a sequence generator in
    my class's constructor. Now merely **loading** a PC class will cause my
    sequence to be incremented (not great), or will throw an exception if the
    environment isn't set up for the sequence generator (terrible). This
    latter problem happens if I enhance my classes, then try to enhance them
    again -- the enhancer tries to load my class, the static { ... } block
    tries to load the sequence generator, and the whole enhancer blows up.
    Questions:
    * Why is this necessary? Why does JDOImplHelper.registerClass() take an
    instance of the class being registered? Could the call to
    JDOImplHelper.registerClass() happen the first time the ctor is called,
    instead of inside a static { ... } block?
    * Given that the above questions probably have reasonable answers, how
    should I work around this problem?
    Thanks,
    Paul

    Hi Patrick,
    Do you understand why jdoNewInstance and jdoNewObjectIdInstance are not static? And if that could
    be done, would that void the need to register the class when loaded?
    David
    Patrick Linskey wrote:
    >
    On 5/28/02 12:39 PM, "Paul Cantrell" <[email protected]> wrote:
    Questions:
    * Why is this necessary? Why does JDOImplHelper.registerClass() take an
    instance of the class being registered? Could the call to
    JDOImplHelper.registerClass() happen the first time the ctor is called,
    instead of inside a static { ... } block?The JDO spec inserts some utility methods into PersistenceCapable classes
    for which it needs a method around.
    These utility methods are the jdoNewInstance() methods and the
    jdoNewObjectIdInstance() methods. These methods improve performance by
    allowing PersistenceCapable objects and application ID OID classes to be
    created without using reflection.
    This class registration must occur at class initialization time, because
    your first operation on the class might very well be to look up objects of
    that class from the data store, in which the no-args constructor might not
    be called.
    * Given that the above questions probably have reasonable answers, how
    should I work around this problem?Your best bet is probably to not do the sequence generation in your no-args
    constructor. One option would be to create a constructor with a marker
    boolean that designates if sequence generation should occur; another
    probably more elegant solution would be to create a factory method that uses
    the default constructor and then assigns an ID, or fetches an ID and then
    invokes a non-default constructor with that ID.
    -Patrick
    Patrick Linskey [email protected]
    SolarMetric Inc. http://www.solarmetric.com

  • Why is doAs() static?

    I've been bothered by this for a while, and I can't come up with a good answer.
    Why is the doAs() method of the Subject class static? It seems totally bizarre to be invoking a static method on that class, then passing a perfectly good instance of the Subject class as the first argument into the call. I know the API specifiers know how to invoke a non-static method using an instance (!! ;) but I'm darned if I can see any reason for them not to have done this.
    Please, help my sanity, what am I missing?
    Cheers,
    Simon

    It is merely static to have a consistent look with java.security.AccessController.doPrivileged.
    As you point out, there is no technical reason it needed to be static.

  • Why is this static import of an Enum not working?

    I get the error:
    State.java:3: cannot find symbol
    symbol: class Command
    import static Command.*;
    ^
    /** FIRST CLASS **/
    import static Command.*;
    public enum State
         Authorization( new Command[] { USER, PASS, APOP, QUIT } ),
         Transaction( new Command[] { STAT, LIST, RETR, DELE, NOOP, RSET, UIDL, TOP, QUIT } ),
         Update( new Command[] { QUIT } );
         private Command[] allowedCommands;
         State(Command[] allowedCommands)
              this.allowedCommands = allowedCommands;
         //Rest is elided...
    /** SECOND CLASS **/
    public enum Command
         USER, PASS, APOP, QUIT, STAT, LIST, RETR, DELE, NOOP, RSET, TOP, UIDL;
    }

    DrClap wrote:
    You forgot to include the name of the package which the Command enum is in. Your import statement should look like this:
    import static package.Command.*where "package" is the name of the package.Yes, I was just testing something with the enums before putting it back into my classes with packages. So the test had no packages. When I retried it in my classes (with packages) it worked. Thanks!
    If Java (i.e. the JVM) is going to have restrictions on things working without packages, why allow not using them at all? Shouldn't it just require packages instead of undocumented and inconsistent restrictions?

  • Why has everything I was following disappeared?

    Whenever I saw a post that I found worthwhile to remember, I marked it as "following" to have it listed in my account content view under "Following", similar to bookmarking, which has only been introduced recently.
    When I browse "following" under my content tab now, it is completely empty: Dude!
    I have previously turned the following of some forum spaces on and off in order to see if it helps to receiving email notifications according to my account preferences, which for some reason did not work, but if this was the cause why I lost the information of everything I also following individually, then I would consider this a real annoying bug or serious shortcoming of the forum software that people should be warned about. Sorry, that's completely useless.

    Dude! wrote:
    When a posting is moved to another forum space the previous Dashboard and Communications alerts are not reset. I've noticed that several times, and that's probably what happened in your case.
    I don't think it's about them being 'reset'... I think it comes down to Jive's concept of 'spaces' rather than forums.  When content is 'moved' it's not really moved... it as if it's 'shared' with the other space, so it becomes visible in both in terms of tracking it... even if the threads primary location resides in another space.  It's as if the Jive developers thought that people would always post things in a suitable space but there was a need to allow things to be shared between spaces if it was relevant to more than one.  It's certainly not a 'move' in the conventional sense of forums (or most other forum software)

  • Restoring--Argghh! Why does everything have to be so difficult?

    I have been thrilled with my new iPhone for almost a month now, however it has crashed completely twice over the last couple of days, (Just the Apple Logo even after a Home/Power reset) It crashed once when I connected to computer and instructed it to update all applications (via app store in iphone.) and another time when it was installing a new application from phone(Movies free version), it just sort of tried to auto reboot and got stuck in the process. It took ages to restore and backup.
    The very next day the same thing happened I was out on the road and I had my laptop with me. so a bit of a pain but not too much of a problem I thought, WRONG Why Oh Why can I not restore it without an Internet connection and insisting it needs to connect for the latest software, OK I appreciate Apple might want to update your phone with the latest versions, but surely there should be an option to bypass this. I was left with no phone for the rest of the day. Surely this is an important issue, supposing you landline happens to go down at the same time your iphone, you are then completely stuffed, Why can you not keep the last downloaded update on the computer, or allow a direct backup. Aargghh!!again
    If this keeps happening it will seriously affect my opinion of the phone which up until now has been favourable.
    Also what could cause my iphone to crash like that, it is too many or erroneous sofware issues. I wonder if it was a memory problem as I believe some applications seem to keep scores/last position etc, does that mean they are all still running in the background? and if so is this the problem?
    Cheers Terry

    Same problem here. Installed an app using the iPhone's App Store and the phone crashes. Then I just get the Apple logo. Then I restore and it tells me all my backups are corrupt. Awful.
    Quite honestly, I've had enough of this. Not had a phone with me all day today or when it happened last week.
    Long time Apple fan but if this isn't remedied before I have another crash, I'm going back to Nokia. Just can't afford to spend days without a phone. I think Apple need to acknowledge the issue and comment on it rather than deleting posts.
    This is crippling my use of the iPhone.

  • Yahoo mailto: is working but not right. Why is everything in the to: spot in the email?

    I got the yahoo into the mailto: but everything is in the To: part of the email. For example the to: has "mailto:[email protected]?subject=xxxxxxxx (city)" I cant figure that out. Any ideas? Thanks!

    OK now I have a far more informed perspective on your question.
    I still do not know if you mean you connect to it via cloud, wireless, or ethernet- you said network- So I will assume a wireless or iCloud file sharing.
    You say it is visible then not visible... I will postulate that you should change the settings to allow screen to sleep, but keep the drive awake.
    I do not know what you have done to try to access it, when it is not visible.. or if you even can.
    If it must be password- opened from sleep, that could present difficulties. So, sleep screen, no sleep drive, and no password for acceptable users, should, I am GUESSING on what I know, allow you to always see it, and access it.

Maybe you are looking for

  • SharePoint 2007:Cannot connect to the configuration database.

    The share point 2007 server under Windows Server 2008 R2 runs well formerly, but suddenly the user can't visit the site with error on page: Cannot connect to the configuration database. I also checked windows log with bellow main errors: Log Name:   

  • Same problem here-Apple TV not in iTunes -7 error ??

    I have the same problem as numerous others, last week neither of my Apple TVs show up on the iTunes device list. So today I unpaired one of them and of course that deletes all content. I unplugged, rebooted, repaired permissions, etc., etc.... Howeve

  • Need to record from Cell phone to my mac.

    Anyone know of a device perhaps from Radio shack that can plug into my cell phone and go into my mac so I can record scratch track VO? I need to hear the Scratch track with an ear piece of my own as it enters my mac.

  • CGContextDrawImage camera app crashes

    Hello All, My first post on this forum. I am trying to obtain an image using the AVCaptureSession. I followed this tutorial http://www.benjaminloulier.com/posts/2-ios4-and-direct-access-to-the-camera. I am creating uiimage from image ref and then obt

  • Tricky problem in JTable

    Hi there, I am trying to make a JTable display the keyText of all the keys pressed, meaning if I write an "A" I want it to be shown as " shift + a ". Now I'm not quite sure about the way to handle the focus and how to trigger of the action that makes