• 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

forward reference doubt

 
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if i can do forward reference as follows:

class X
{
int i = j = 20; // forward reference. OK
int j = 10;
}

then why cannot i do forward reference as below:

class X
{
int i;
i = j = 20; // forward reference. Not OK
int j = 10;
}
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Vikas,

You cant use the variable j before declaring it.You have declared the variable j after assigning it to i in the second line..Got it???
[ June 28, 2007: Message edited by: Nithya Kumar ]
 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Vikash,

Both code provided by you will give compile time error.

Neither of forward reference is OK in your code.Check it.

(Because compiler will complain about variable j, which is used before it's declaration)
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See this


Thanks,
 
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First class will compile fine, variable j is instance variable so its scope is the entire class no matter where it is declared unlike local
 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And in the second class, the problem is not the forward reference.
Use the following instead.
 
vikas sharmaa
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ahmed Yehia:
First class will compile fine, variable j is instance variable so its scope is the entire class no matter where it is declared unlike local



but second class should also compile fine for the same reason. why not?
 
vikas sharmaa
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Manfred Klug:
And in the second class, the problem is not the forward reference.



but if am combining declaration and assignment it's compiling without any error. why is it so.

int i = j = 20; // here assignment ok. why?
int j;
 
vikas sharmaa
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Chandra Bhatt:
See this


Thanks,



good link. but my query is not answered there too.
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Vikas you declared the variable and assigned it. This works fine...
but with the previous the variable was not declared and how can you assign it??
 
Manfred Klug
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by vikas chess:
but if am combining declaration and assignment it's compiling without any error. why is it so.



Since all you can do at this point is:
  • write instance initializers
  • write static initializers
  • declare constructors
  • declare fields
  • declare methods
  • declare classes
  • declare interfaces

  • 'int i = j = 20' is a field declaration which is ok.
    'i = j = 20' is nothing of the above, and so not allowed.
     
    vikas sharmaa
    Ranch Hand
    Posts: 191
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Originally posted by Raghu Doppalapudi:
    but with the previous the variable was not declared and how can you assign it??



    but i declared the variable a line before. and then assigning it. whats wrong in it.

    case 1:

    int j = i = 20 // declared and assigned (OK)
    int i = 10

    case 2:

    int j; // declared
    j = i = 20; // assigned (Not OK)
    int i = 10;

    case 1 and 2 are very similar. then why case 2 not OK
     
    vikas sharmaa
    Ranch Hand
    Posts: 191
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Originally posted by Manfred Klug:


    Since all you can do at this point is:

  • write instance initializers
  • write static initializers
  • declare constructors
  • declare fields
  • declare methods
  • declare classes
  • declare interfaces

  • 'int i = j = 20' is a field declaration which is ok.
    'i = j = 20' is nothing of the above, and so not allowed.


    thanks klug,

    i got it now.
     
    Ranch Hand
    Posts: 18944
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi All,

    After going through the entire thread, I got one more query into picture.

    What is the meaning of int i=j=10; does this mean that we are declaring both i and j which are int simultaneously and assigning the value 10;

    If the above statement is correct, then
    int i=j=10; /* Is this equivalent to int i=10; int j=10; */
    int i=20;

    then because of the next statement int i=20; does not result in compile time error ( i is already defined in AnimalThread).

    This doubt may look very dumb, but I'm really curious to know the answer. Thanks in advance.
     
    Manfred Klug
    Ranch Hand
    Posts: 377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi Chandra,

    int j = i = 20 is equivalent to:

    i = 20;
    int j = i;
     
    Anonymous
    Ranch Hand
    Posts: 18944
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Oh Now I understood. Thanks Manfred.
     
    Ranch Hand
    Posts: 637
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator


    int j = i = 20 is equivalent to:

    i = 20;
    int j = i;



    How is this possible , Shoudn't the compiler throw an error @ i=20, Variable i is not declared. And hence when we do int i = j = 20, It should throw an error saying j is not declared and hence cannot be assigned.

    Also what if i do
    int j = i = 20 is equivalent to:
    Test i;
    Is this an error?


    Thanks
    Deepak
     
    Manfred Klug
    Ranch Hand
    Posts: 377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Originally posted by Deepak Jain:
    How is this possible , Shoudn't the compiler throw an error @ i=20, Variable i is not declared. And hence when we do int i = j = 20, It should throw an error saying j is not declared and hence cannot be assigned.

    Have a look at Forward Referencing.

    Also what if i do
    int j = i = 20 is equivalent to:
    Test i;
    Is this an error?

    Try it.
     
    If you're gonna buy things, buy this thing and I get a fat kickback:
    Garden Master Course kickstarter
    https://coderanch.com/t/754577/Garden-Master-kickstarter
    reply
      Bookmark Topic Watch Topic
    • New Topic