• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Forward References in Java

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Launchers,

Is threre any forward references in java? In khalid Mughal scjp1.4 book , in "initializers" chapter he gave some programs .The programs are not run in my system and I get Compilation Errors.

For Ex:

class Initialization
{
private static String msg(String msg)
{
System.out.println(msg); return msg;
}
--------------(1)
public Initialization() { m=msg("1") ; }
{ m=msg("2") ; }
String m=msg("3");
public static void main(String args[])
{
Object obj = new Initialization();
}
}

In this If I declare the String var. at (1) Compiled otherwise errors occured.


Thanks for all suggestions,

[email protected]
 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

original code
class Initialization
{
private static String msg(String msg)
{
System.out.println(msg); return msg;
}
--------------(1)
public Initialization()
{//2
m=msg("1") ; //5
}
{
//3
m=msg("2") ;
}
String m=msg("3"); //4
public static void main(String args[])
{
Object obj = new Initialization(); //1
}
}



Here first look at the order in which code is executed.
In main method, at the time of creating object obj, its constructor is called.Look at the numbers provided by me, after that control goes to //2 i.e constructors first line. after that control moves to initializer block at //3. after that a string var at //4. and then return back to constructor.

We havent declared var m, before executing //3. m is declared at //4.
Here at //3, the var m is undeclared, hence it is giving compile time error of illegal forward reference.

if we change the code at //3 like
String m=msg("2") ;
then there wont be any error.
I hope u get it
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Swati,
Doesnt the the declaration need to be in line //2:
String m= msg("1") inside the constructor

Thanks
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code provided here should not give any compilation errors. It would compile and run just fine. The output would be:

2
3
1

The order of execution would be:

1. initilization block execution.
2. member variable assignment.
3. constructor call.

Hope this is helpful.
[ January 12, 2006: Message edited by: Ram Roop ]
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ram Roop:
The code provided here should not give any compilation errors. It would compile and run just fine. The output would be:

2
3
1

The order of execution would be:

1. initilization block execution.
2. member variable assignment.
3. constructor call.

Hope this is helpful.

[ January 12, 2006: Message edited by: Ram Roop ]




Agree, as it doesn't violate declare-before-read rule.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so it seems forward referencing is allowed because during initializer block execution, variable m is not declared.Is there any scenario in java where forward referencing not allowed?.
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have confirmed, code is compiling fine(not giving any error).
Out put is
2
3
1

Thanks
Gagan.
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you try to use a varible in your code before you declare it you will get an compile error.

declaring the code

foo = "here is a string";
String foo= new String();

will give you a complie error you can not create forward reference in this manner.
 
reply
    Bookmark Topic Watch Topic
  • New Topic