Interview Question: Initialization in a Class

Sample Question #237 (programming – C++ or Java)

What’s wrong with the following class definition?

class MyClass {
  int i = 0;
  int name = ‘Britney’;
}
Advertisements
This entry was posted in Sample Qs. Bookmark the permalink.

3 Responses to Interview Question: Initialization in a Class

  1. Brett says:

    ANSWER
     
    Totally fine in Java!
     
    But two problems if this is C++ code:
     
    1. You cannot initialize class variables inline; you must use a constructor.
    2. The code is missing a semicolon after the closing brace for the class definition.
     

  2. Tuyen says:

    Most compilers support initializations of const static integral types within the class declaration.

  3. says:

    well ,  why the

      int name = ‘Britney’;
    is correct ?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s