Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8516429
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T05:25:32+00:00 2026-06-11T05:25:32+00:00

I have 2 pieces of code from Java: class Test1 { index = 1;

  • 0

I have 2 pieces of code from Java:

class Test1 {
  index = 1;
  int index;
}

versus

class Test2 {
  { index = 1; }
  int index; 
}

the first one failed when compiling, I guess it’s because the member “index” is used before it is defined? The second one is error-free. I don’t understand why. Can you guys spare some minutes and help me? Thanks a lot!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T05:25:34+00:00Added an answer on June 11, 2026 at 5:25 am

    The second one compiles, because that { … } inner construction present in the second code is a so-called initializer block. This block is copied into each constructor by Java compiler.

    And, as any other class method, it can use a property defined in that class; it shouldn’t matter where exactly this property is defined – above or before this block.

    The first one does not compile, because it simply does not fit into the language specification for class bodies and member declarations, which defines basically seven allowed things:

    1. InstanceInitializer (the construct used here)
    2. StaticInitializer (similar to first, but executed on class loading)
    3. ConstructorDeclaration
    4. FieldDeclaration
    5. MethodDeclaration
    6. ClassDeclaration
    7. InterfaceDeclaration
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using iTextSharp in VB.net. I have this piece of code from java
I have the following piece of Java code that reads strings from CSV file.
I have the following snippet of java code: final Class<?> junitCoreClass = AccessController.doPrivileged( new
I have this piece of code in my java class mystring = mysuperstring.split(/); I
Ok i have this piece of code from which i took from W3schools :-
I have the following piece of code from a function that takes the host
I have the following piece of code from a book. There is this function
I have the following piece of code taken from the PHP manual on the
I have a piece of code that takes several rows from a database and
If have a piece of code that gets some data from a sql database

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.