• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Rob Spoor
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Carey Brown
Bartenders:

Static

 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Code:
----------------------------------------------------------------
public class Class20 {
static int p = abc();

static public int abc() {
int i = 123; //1
System.out.println(p);
return i;
}

public static void main(String arg[]) {
}
}
----------------------------------------------------------
Query: The above code is compiling successfully. My confusion is about the variable 'int i'? Wont it be a instance variable or
all those variables declared in static code are static by default

Please help me to clarify my doubt.
 
Ranch Hand
Posts: 340
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ritu,
I dont think that all the variables declared in static code are static by default. Infact they are just automatic variables.
Similarly members declared in Nested Top Level classes are also not static by default.
 
Sandeep Chhabra
Ranch Hand
Posts: 340
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can also refer to this topic for detailed explinatin
https://coderanch.com/t/250990/java-programmer-SCJP/certification/confusion-top-level-nested-or
 
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Here i is just a variable local to the scope of the method...and not static

If it has to be static you need to define it as a class level variable..

reply
    Bookmark Topic Watch Topic
  • New Topic