I have the following code:  public class Book {     private static int sample1(int i) {         return i++;     }     private static int sample2(int j) {         return ++j;     }      public static void main(String[] arguments){          int i = 0;         int j = 0;         System.out.println(sample1(i++)); //0         System.out.println(sample1(++i)); //1         System.out.println(sample2(j++));//1         System.out.println(sample2(++j));//2         System.out.println(i);//2         System.out.println(j);//2     } }   My expected output is in comments. The actual output is below:  0 2 1 3 2 2   I'm getting confused with the function calls and incemental operator. Can someone kindly explain the actual result?
121
I always thought Java uses pass-by-reference. However, I've seen a couple of blog posts (for example, ) that claim that it isn't (the blog post says that Java uses pass-by-value). I don't think I understand the distinction they're making. What is the explanation?
91389
I can't seem to access instance members of the surrounding class from inside an enum, as I could from inside an inner class. Does that mean enums are static? Is there any access to the scope of the surrounding class's instance, or do I have to pass the instance into the enum's method where I need it?  public class Universe {     public final int theAnswer;      public enum Planet {         // ...         EARTH(...);         // ...          // ... constructor etc.          public int deepThought() {             // -&gt; "No enclosing instance of type 'Universe' is accessible in this scope"             return Universe.this.theAnswer;         }     }      public Universe(int locallyUniversalAnswer) {         this.theAnswer = locallyUniversalAnswer;     } }
82054
While going through apex pdf i got line      Static variables defined in a trigger don’t retain their values   between different trigger contexts".   Can anyone please explain me with some code sample. I am confused as to what is the meaning of defining a static variable inside a trigger. Static variables are are used in Class context. So what a static variable does in trigger context.
24968
If a variable is declared as static in a function's scope it is only initialized once and retains its value between function calls. What exactly is its lifetime? When do its constructor and destructor get called?  void foo()  {      static string plonk = "When will I die?"; }
138
What is the main difference between an inner class and a static nested class in Java? Does design / implementation play a role in choosing one of these?
6108
Why do I get compiler errors with this Java code?  1  public List&lt;? extends Foo&gt; getFoos() 2  { 3    List&lt;? extends Foo&gt; foos = new ArrayList&lt;? extends Foo&gt;(); 4    foos.add(new SubFoo()); 5    return foos; 6  }  Where 'SubFoo' is a concrete class that implements Foo, and Foo is an interface.  Errors I get with this code:   On Line 3: "Cannot instantiate ArrayList&lt;? extends Foo&gt;" On Line 4: "The method add(capture#1-of ? extends Foo) in the type List&lt;capture#1-of ? extends Foo&gt; is not applicable for the arguments (SubFoo)"   Update: Thanks to Jeff C, I can change Line 3 to say "new ArrayList&lt;Foo&gt;();".  But I'm still having the issue with Line 4.
78162
I collect a few corner cases and  and would always like to hear more. The page only really covers C# language bits and bobs, but I also find core .NET things interesting too. For example, here's one which isn't on the page, but which I find incredible:  string x = new string(new char[0]); string y = new string(new char[0]); Console.WriteLine(object.ReferenceEquals(x, y));   I'd expect that to print False - after all, "new" (with a reference type) always creates a new object, doesn't it? The specs for both C# and the CLI indicate that it should.  Well, not in this particular case. It prints True, and has done on every version of the framework I've tested it with. (I haven't tried it on Mono, admittedly...)  Just to be clear, this is only an example of the kind of thing I'm looking for - I wasn't particularly looking for discussion/explanation of this oddity. (It's not the same as normal string interning; in particular, string interning doesn't normally happen when a constructor is called.) I was really asking for similar odd behaviour.  Any other gems lurking out there?
11329
I'm looking at some open source Java projects to get into Java and notice a lot of them have some sort of 'constants' interface.  For instance,  has an interface called , and most other core classes implement this interface. The interface is riddled with static members. Is there a reason for this approach, or is this considered bad practice? Why not use enums where it makes sense, or a static class?  I find it strange to use an interface to allow for some sort of pseudo 'global variables'.  public interface PConstants {    // LOTS OF static fields...    static public final int SHINE = 31;    // emissive (by default kept black)   static public final int ER = 32;   static public final int EG = 33;   static public final int EB = 34;    // has this vertex been lit yet   static public final int BEEN_LIT = 35;    static public final int VERTEX_FIELD_COUNT = 36;     // renderers known to processing.core    static final String P2D    = "processing.core.PGraphics2D";   static final String P3D    = "processing.core.PGraphics3D";   static final String JAVA2D = "processing.core.PGraphicsJava2D";   static final String OPENGL = "processing.opengl.PGraphicsOpenGL";   static final String PDF    = "processing.pdf.PGraphicsPDF";   static final String DXF    = "processing.dxf.RawDXF";     // platform IDs for PApplet.platform    static final int OTHER   = 0;   static final int WINDOWS = 1;   static final int MACOSX  = 2;   static final int LINUX   = 3;    static final String[] platformNames = {     "other", "windows", "macosx", "linux"   };    // and on and on  }
16225
Why does it (apparently) make a difference whether I pass null as an argument directly, or pass an Object that I assigned the value null?  Object testVal = null; test.foo(testVal);    // dispatched to foo(Object) // test.foo(null);    // compilation problem -&gt; "The method foo(String) is ambiguous"     public void foo(String arg) { // More-specific     System.out.println("foo(String)"); }  public void foo(Object arg) { // Generic     System.out.println("foo(Object)"); }   In other words, why is the (commented-out) second call to foo(...) not dispatched to foo(Object)?  Update: I use Java 1.6. I could compile Hemal's code without problems, but mine still doesn't compile. The only difference I see is that Hemal's methods are static while mine are not. But I really don't see why this should make a difference...?  Update 2: Solved. I had another method foo(Runnable) in my class, so the dispatcher couldn't unambiguously select the single most specific method. (See my comment in Hemal's second answer.) Thanks everyone for your help.
82429
Why can't enum's constructor access static fields and methods? This is perfectly valid with a class, but is not allowed with an enum.  What I'm trying to do is store my enum instances in a static Map. Consider this example code which allows lookup by abbreivation:  public enum Day {     Sunday("Sun"), Monday("Mon"), Tuesday("Tue"), Wednesday("Wed"), Thursday("Thu"), Friday("Fri"), Saturday("Sat");      private final String abbreviation;      private static final Map&lt;String, Day&gt; ABBREV_MAP = new HashMap&lt;String, Day&gt;();      private Day(String abbreviation) {         this.abbreviation = abbreviation;         ABBREV_MAP.put(abbreviation, this);  // Not valid     }      public String getAbbreviation() {         return abbreviation;     }      public static Day getByAbbreviation(String abbreviation) {         return ABBREV_MAP.get(abbreviation);     } }   This will not work as enum doesn't allow static references in its constructor. It however works just find if implemented as a class:  public static final Day SUNDAY = new Day("Sunday", "Sun"); private Day(String name, String abbreviation) {     this.name = name;     this.abbreviation = abbreviation;     ABBREV_MAP.put(abbreviation, this);  // Valid }
101830
There is a Java Void -- uppercase V-- . The only situation I have ever seen it used is to parameterize Callables    final Callable&lt;Void&gt; callable = new Callable&lt;Void&gt;() {             public Void call() {                 foobar();                 return null;             }         };   Are there any other uses for the Java Void reference type? Can it ever be assigned anything other than null? If yes, do you have examples?
5812
Why does the  exist in modern languages such as C# and Java? Is it purely a self documenting code feature, or does it serve any actual purpose?  For instance the following example:  Class1 obj = new Class1();  Class1 foo() {     return new Class1(); }   Is as easy to read as the more Pythonesque way of writing it:  Class1 obj = Class1();  Class1 foo() {     return Class1(); }   EDIT:  hit the nail on the head with the clarification of the question: Why did they choose this syntax?
2689
What is the idiomatic Python equivalent of this C/C++ code?  void foo() {     static int counter = 0;     counter++;     printf("counter is %d\n", counter); }   specifically, how does one implement the static member at the function level, as opposed to the class level? And does placing the function into a class change anything?
12184
I'm trying to get an understanding of what the the java keyword this actually does. I've been reading Sun's documentation but I'm still fuzzy on what this actually does.
56900
Here's what :   static class CompanyInfo {     public static string GetCompanyName() { return "CompanyName"; }     public static string GetCompanyAddress() { return "CompanyAddress"; }     //... }       Use a static class as a unit of   organization for methods not   associated with particular objects.   Also, a static class can make your   implementation simpler and faster   because you do not have to create an   object in order to call its methods.   It is useful to organize the methods   inside the class in a meaningful way,   such as the methods of the Math class   in the System namespace.   To me, that example doesn't seem to cover very many possible usage scenarios for static classes. In the past I've used static classes for stateless suites of related functions, but that's about it. So, under what circumstances should (and shouldn't) a class be declared static?
9423
I have just discovered this feature.   Declaring an interface using the "@interface" syntax allows you to put a default value.  public @interface HelloWorld {       public String sayHello() default "hello world"; }   This is something new for me. How is that default value suppose to be used.  I cannot find references to that, because the www is full of java interface documents prior to "@" addition in Java 1.5 ( was it on .5 or in .4? )     EDIT  Thanks for the answers ( I was somehow close to "annotation", for I use the tag already )  :P   I knew I should've read that document years ago!!!... let's see...      Many APIs require a fair amount of boilerplate code. For....
57401
I have been working on a Java project for a class for a while now. It is an implementation of a linked list (here called AddressList, containing simple nodes called ListNode). The catch is that everything would have to be done with recursive algorithms. I was able to do everything fine sans one method: public AddressList reverse()      ListNode:    public class ListNode{   public String data;   public ListNode next; }   Right now my reverse function just calls a helper function that takes an argument to allow recursion.  public AddressList reverse(){   return new AddressList(this.reverse(this.head)); }   With my helper function having the signature of private ListNode reverse(ListNode current).  At the moment, I have it working iteratively using a stack, but this is not what the specification requires. I had found an algorithm in C that recursively reversed and converted it to Java code by hand, and it worked, but I had no understanding of it.  Edit: Nevermind, I figured it out in the meantime.  private AddressList reverse(ListNode current, AddressList reversedList){   if(current == null)        return reversedList;   reversedList.addToFront(current.getData());   return this.reverse(current.getNext(), reversedList); }   While I'm here, does anyone see any problems with this route?