Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within OCPJP
Search Coderanch
Advance search
Google search
Register / Login
Win a copy of
DevSecOps Adventures: A Game-Changing Approach with Chocolate, LEGO, and Coaching Games
this week in the
Agile and Other Processes
forum!
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
Paul Clapham
Devaka Cooray
Tim Cooke
Sheriffs:
Rob Spoor
Liutauras Vilda
paul wheaton
Saloon Keepers:
Tim Holloway
Tim Moores
Mikalai Zaikin
Carey Brown
Piet Souris
Bartenders:
Stephan van Hulst
Forum:
Programmer Certification (OCPJP)
Doubt Reg: initializing Variables with method call
Ravi Pinnaboyina
Greenhorn
Posts: 11
posted 17 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
How the output is printed as Zero.......
How is the value of i being resolved ?
private int i=getData();
private int j=10;
int getData()
{
return j;
}
public static void main(
String
arg[])
{
System.out.println(""+new ASample().i);
}
Keith Lynn
Ranch Hand
Posts: 2412
posted 17 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
The value of j is read in the method before it has been initialized.
Ravi Pinnaboyina
Greenhorn
Posts: 11
posted 17 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
So it means that i and j will only be declared first only then assignment starts...
i.ie.,
int a=10;
int b=10;
first a nd b will be declared only then they get initialized???
Am I right??
Keith Lynn
Ranch Hand
Posts: 2412
posted 17 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
The instance variables are in scope, but initialization occurs from top to bottom in the class definition. Since the call to the method accesses j before it is initialized, it has its default value.
Anton Uwe
Ranch Hand
Posts: 122
posted 17 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
public class ASample { private int i=getData(); private int j=10; int getData() { return j; } public static void main(String arg[]) { System.out.println(""+new ASample().i); } }
is the same as
public class ASample { private int i=0; private int j=0; public ASample() { super(); i=getData(); j=10; } int getData() { return j; } public static void main(String arg[]) { System.out.println(""+new ASample().i); } }
We find this kind of rampant individuality very disturbing. But not this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
default constructor
JTable method to return table body
Reading array from file
forward refrerences
A basic question..
More...