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

  • SEARCH
  • Home
  • 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 8697011
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:24:40+00:00 2026-06-13T01:24:40+00:00

As static block is used to initialize static data member in dynamic way, I

  • 0

As static block is used to initialize static data member in dynamic way, I have this code:

class Temp {
    static int x;

    static {
        try {
            x = System.in.read();
        } catch (Exception e) {
            //Do nothing
        }
    }
}

class Temp1 {
    public static void main(String args[]) {
        System.out.println(Temp.x);
    }
}

class Temp2 {
    public static void main(String args[]) {
        System.out.println(Temp.x);
    }
}

While running Temp1, Temp2 the normal value of x should be what I entered from keyboard, but I got 49 and 50 always no matter whatever I entered from keyboard.

I already see the read() method of inputstream class it should return the same. Why it is returning 49 and 50 in every case?

  • 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-13T01:24:42+00:00Added an answer on June 13, 2026 at 1:24 am

    The direct question you are asking is not really the first thing that you should be informed of here because the very approach you are taking is quite misguided:

    1. Never use class initializers to read from keyboard. There is simply never a good reason to do this.
    2. Don’t swallow exceptions: you are shooting yourself in the foot. If there is an error, you won’t be able to diagnose.
    3. You probably expect to parse an integer input at keyboard; you are actually only reading a single byte from the standard input (System.in is a byte stream) and storing it as an int.
    4. You have two identical classes, with two identical main methods in them. This makes no sense: nothing will be “done twice” in any sort of meaning with that code.

    This list is by no means exhaustive, it’s just what I managed to think of right now.

    To conclude, you should redesign your code in the first place just to make the basic approach sane, and only then try to make it work.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When i have a code block static void Main() { foreach (int i in
Hi I have following code block public class Driver { static String x =
I have a class in which i have intialized hashmap in static block. Passing
I have a static initialization block where I read data from DB into certain
I have a block of C# that looks like this public static string[,] FileTypesDict
This code sometime throwing an Exception even i have used synchronized Method removeFirst within
As far as I understood the static initialization block is used to set values
Are variables declared inside a static block accessible anywhere else? What kind of member
I have intialized Hash map inside static block, I need to access the hashmap
I want to give a static javascript block of code to a html template

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.