Results 1 to 11 of 11

Thread: Simple Question

  1. #1
    jmohandos304 is offline Senior Member
    Join Date
    Apr 2014
    Posts
    122
    Rep Power
    0

    Default Simple Question

    Code 1 compiles, but Code 2 does not. Why?

    Code 1:
    Java Code:
    class A 
    {
    	static A a = new A();
    	static B b = new B();
    	public static void main(String[] args) 
    	{
    		a=b;
    	}
    };
    class B extends A {}
    Code 2:
    Java Code:
    class A 
    {
    	static A a = new A();
    	static B b = new B();
    	a=b;
    	public static void main(String[] args) 
    	{
    
    	}
    };
    class B extends A {}

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,807
    Rep Power
    12

    Default Re: Simple Question

    What does the error message say? Always, always, always read the error message first. Java error messages are very informative in my opinion.

    Anyhoo, you cannot have a standard statement such as "a=b;" outside a method, constructor or static block.

  3. #3
    jmohandos304 is offline Senior Member
    Join Date
    Apr 2014
    Posts
    122
    Rep Power
    0

    Default Re: Simple Question

    The error message says:
    Syntax error on token ";", , expected

    Also, when a statement like static A a = new A(); is allowed outside a method, then why not the statement a=b

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,807
    Rep Power
    12

    Default Re: Simple Question

    As I said static statements are allowed outside methods and constructors.

  5. #5
    jmohandos304 is offline Senior Member
    Join Date
    Apr 2014
    Posts
    122
    Rep Power
    0

    Default Re: Simple Question

    Pls check the below code. It compiles fine.

    Java Code:
    class A 
    {
        static A a = new A();
        static B b = new B();
        C c =new C();
        public static void main(String[] args) 
        {
     
        }
    };
    class B extends A {}
    class C{}

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,807
    Rep Power
    12

    Default Re: Simple Question

    Do you have a question?

  7. #7
    jmohandos304 is offline Senior Member
    Join Date
    Apr 2014
    Posts
    122
    Rep Power
    0

    Default Re: Simple Question

    Back to my first post:

    Code 1 compiles, but Code 2 does not. Why?

    If code like C c =new C(); can be placed outside any method then why not a=b

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    26

    Default Re: Simple Question

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  9. #9
    jmohandos304 is offline Senior Member
    Join Date
    Apr 2014
    Posts
    122
    Rep Power
    0

    Default Re: Simple Question

    A class body may contain declarations of members of the class, that is, fields (8.3), methods (8.4), classes (8.5), and interfaces (8.5).

    A class body may also contain instance initializers (8.6), static initializers (8.7), and declarations of constructors (8.8) for the class.


    Are the above the lines the one to refer to? If yes, what is the difference between instance initialisers and declaration of constructors?

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Simple Question

    (static) variables can be declared outside the body of a method; it makes them (static) member variables; a declaration may involve an initialization which is an arbitrary expression of the correct type; 'a= b;' isn't a declaration; it is an ordinary assignement statement/expression.

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  11. #11
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Simple Question

    Quote Originally Posted by jmohandos304 View Post
    A class body may contain declarations of members of the class, that is, fields (8.3), methods (8.4), classes (8.5), and interfaces (8.5).

    A class body may also contain instance initializers (8.6), static initializers (8.7), and declarations of constructors (8.8) for the class.


    Are the above the lines the one to refer to? If yes, what is the difference between instance initialisers and declaration of constructors?
    ... those X.X thingies in your copy/pasted text are clickable links which explain what each individual term referred to is. When you learn that with enough focus and patience that you actually understand it, you automatically also learn the difference.

    At the very least, those terms you refer to can be put into google with the word "java" in front of it for further reading.
    Last edited by gimbal2; 09-11-2014 at 10:39 AM.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Simple question
    By Games2Design in forum New To Java
    Replies: 10
    Last Post: 09-30-2012, 08:54 PM
  2. simple question
    By jwl in forum New To Java
    Replies: 4
    Last Post: 08-05-2012, 03:31 AM
  3. A simple question
    By Rylogy in forum AWT / Swing
    Replies: 2
    Last Post: 02-15-2012, 02:54 AM
  4. Simple Question
    By stackptr89 in forum New To Java
    Replies: 13
    Last Post: 01-29-2011, 06:35 PM
  5. Simple Question
    By barusk in forum Networking
    Replies: 13
    Last Post: 03-04-2009, 08:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •