-1

I know why this is error in C++.

class A
{
     A b;
};
int main() {}

Because the type of b is unresolved at the compile time. Then why is the below code correct??

class A
{
     static A b;
}
int main() {}

Is the type of the variable b resolved if it is static??

And lastly to my surprise the following code was working for JAVA:

public class Main
{
Main g;
public static void main(String[] args)
{
    System.out.println("hi");
}
}

Why is that so? I know about static variables that they are specific to a class and not to an instance. But that doesn't give the answer to the above question.... Please throw me some light.

Andy Prowl
  • 124,023
  • 23
  • 387
  • 451
nitish712
  • 19,504
  • 5
  • 26
  • 34
  • 4
    Please **read a good *C++ programming book*** before asking such questions here.... We cannot teach you C++ in a few minutes, you need to take many hours to learn, and you need to experiment. Enable all warnings in your compiler. – Basile Starynkevitch May 13 '13 at 11:02
  • Java's variables are more like C's pointers in that case. – Elazar May 13 '13 at 11:03
  • 1
    FYI: `static` has absolutely nothing to do with type resolution. – Marko Topolnik May 13 '13 at 11:03
  • 1
    Java exists in a world of its own and is very different from C++. – Greg May 13 '13 at 11:04
  • @all I am keen about the contents of the static variable. I know its kept above the process stack along with global variables, but what can be the contents in the above cases? – nitish712 May 13 '13 at 11:06
  • @nitish712 Where the static variable will reside depends on the implementation; on most of the systems I've worked on, it is _below_ the stack. But the important thing is that it is not in the stack, nor in the heap. And the contents depend on how it is initialized, and how it is modified afterwards. – James Kanze May 13 '13 at 11:09

2 Answers2

6

Because:

static A b;

(in the class itself) is not a definition, so the complete definition of the class isn't necessary.

Java has different rules, and doesn't have the concept of an incomplete type. And of course, variables in Java are never user defined object types, but references (more like C++ pointers) to object types. The equivalent in C++ to your variable declaration in Java would be Main* g;.

James Kanze
  • 150,581
  • 18
  • 184
  • 329
  • 1
    Your answer and [this](http://stackoverflow.com/questions/1410563/what-is-the-difference-between-a-definition-and-a-declaration) has helped me solve this doubt. (I forgot that a static variable is declared first and then defined) Thnaks alot. :) – nitish712 May 13 '13 at 11:10
6

Because the type of b is unresolved at the compile time.

No, it's because the type of b is incomplete at that point. You can declare a variable of incomplete type, but not define one. A definition allocates the space to store the object, and for that the object's size must be known.

The static member declaration is just a declaration; the variable must be defined elsewhere, if it's used. So it's fine to declare that within the class.

The non-static member declaration is also a definition; it allocates space for the variable within the class, and for that the type must be complete. (Also, it's logically impossible for a non-empty object to contain an object of the same type).

Java has no concept of incomplete types, and allows any class to contain (references to) any other class.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644