Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Initialization in class

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Initialization in class

    Hello everyone I am a beginner and this is my first post on this forum and also english is not my first language but I hope it is good enough to make you guys understand my question.

    The question is why I get error when initializing a variable like this in a class ?
     
    public class MyClass {
     
    	int a;
    	a=5;  // error
     
    }

    I don't get error when doing it like this.

     
    public class MyClass {
     
    	int a = 5;
     
     
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Initialization in class

    Declaring a variable and initializing in a single line is allowed outside of a method. Assigning a value to a variable other than during a single-line declaration and initialization is not allowed outside of a method or an initialization block.

  3. #3
    Junior Member
    Join Date
    Sep 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Initialization in class

    Thanks for the answer.

  4. #4
    Junior Member
    Join Date
    Nov 2013
    Posts
    6
    My Mood
    Cheerful
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Initialization in class

    Since you said you were a beginner, here is a little information that might help you out or anyone else who happens to look at this post.

    You really don't want to initialize a variable outside a constructor unless it is a final. Also you need an access modifiers as well.

    public class MyClass {
     
    	private final int a = 5;
     
    }

    otherwise you would do,

    public class MyClass {
     
           private int a;
     
           public MyClass() {
                 a = 5;
           }
     
    }

    hopefully you find this information useful

Similar Threads

  1. Re: JUnit initialization error
    By Shaurabh Prakash in forum Java IDEs
    Replies: 1
    Last Post: September 13th, 2012, 12:34 PM
  2. Problem with array initialization
    By ur2cdanger in forum Collections and Generics
    Replies: 2
    Last Post: April 4th, 2012, 05:07 AM
  3. Inner class initialization and declaration
    By longwu in forum Object Oriented Programming
    Replies: 2
    Last Post: August 31st, 2011, 07:44 AM
  4. Initialization of a class
    By Merik in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 28th, 2010, 07:18 PM
  5. Initialization parameter of servlets
    By rakesh in forum Java Servlet
    Replies: 3
    Last Post: April 13th, 2010, 03:14 AM