Maynia's Profile User Rating: -----

Reputation: 9 Worker
Active Posts:
48 (0.03 per day)
17-March 09
Profile Views:
Last Active:
User is offline Today, 09:45 AM

Previous Fields

Dream Kudos:

Latest Visitors

Icon   Maynia has not set their status

Posts I've Made

  1. In Topic: question regarding static fields and inheritance

    Posted 17 Apr 2014

    This is one of those things that's not particularly intuitive, I'll give it that.

    Broadly, you can treat static fields as not inherited - the definition of a static field requires that there be only one for the entire type, and all classes that inherit from type A are ultimately of type A. HOWEVER - say you have a class A with static int variable x and an inheriting class B, the JVM/compiler will let you get away with doing this:
    B b = new B();
    int test = b.x;

    which looks for all the world like an instance variable. It isn't - the call to
    is treated in exactly the same way as the static call

    Good IDEs will give you a warning about this - something along the lines of 'static fields should be accessed in a static way' but it is only a warning, not an error. There are ways to finagle static variables into behaving similarly to instance variables, but it generally isn't a good idea to try it.

    One more interesting oddity of this - it allows you to look like you're referencing null!
    B b = null;
    int test = b.x;

    Again, that will compile and run, fetching the value of the static field x from the type A - but it's downright ugly.
  2. In Topic: Can't seem to initialize a integer when I already have

    Posted 14 Jan 2014

    No problem, I'll go a bit more in-depth, see if I can explain it satisfactorily. What's important to understand here is that code (within a method anyway) is executed in strict order. What you're doing, in something closer to English, is:

    Creating a space in memory for a String - its value at this point is null - technically it's the default value for all variables of reference types but you can think of it as 'nothing' because it essentially is.

    You then call the String method length() on it - its value is still null at this time, so the request makes no sense (what is the length of nothingness?) - the variable has not been initialized hence the error.

    When, further down, you directly assign a value to it via the line
    word =

    then and only then does its value change from null to something you can validly call String methods on.

    To fix this, you must make sure that the value of a variable cannot possibly be null when you call methods on it. In your case, you could call the length method later, or you could call the input method earlier - it's entirely up to you.

    I could have just said 'yes' here but I'm trying to help you understand what's happening, which will get you further in future.
  3. In Topic: Can't seem to initialize a integer when I already have

    Posted 14 Jan 2014

    At this point:

    String word;
    int lengthOfString = word.length();

    you're calling the method length() on word. The variable is only initialised here:
    word =;

    several lines after length is called on it. That's why you're getting the error - you're calling length on null early on.
  4. In Topic: Replacing one output with another using counter and if

    Posted 16 Dec 2013

    Nesting ifs and indeed nesting generally can be a bit confusing at first, but if you keep doing it you'll get a feel for flow of control and how it works.

    On topic, I think I might have misunderstood the problem a touch (don't post while tired! :whistling:)

    What you have now does this:

    IF the answer given is correct:
    Check if it matches one of the predefined triggers and if so print a message about that

    Print the generic correct message
    IF the answer is not correct print the failure message

    We'll leave compound Boolean statements alone for the moment and try and get this logic right.

    In English, what you want to do is presumably more like this:

    IF the answer given is correct:
    IF one of the predefined triggers matches print a specific message
    OTHERWISE: Print the generic correct message

    OTHERWISE: Print the failure message

    In the event that the answer is correct, we want exactly one thing to happen, whatever it might be. So entirely within the first if, we can use this structure:

    if (answer == correctanswer) {
      if (noOfCorrectAnswers == 2) {
      else if (noOfCorrectAnswers == 4) {
      else {
    else {

    The if-else if-else structure allows us to pick from a number of specific responses - first the outer if is evaluated, and if the condition matches, we evaluate the inner if conditions - only one of these conditions can trigger, and if none of them do we end up taking the generic option. If the outer if condition doesn't match, we won't even try any of the inner ifs, because we don't need to.

    I'm aware this is a bit rambly, because I don't know exactly what your level is, but I hope it helps illuminate a bit more - I should really have mentioned else-ifs in the first post.
  5. In Topic: Replacing one output with another using counter and if

    Posted 15 Dec 2013


    I even tryed some fancy if ((answ == result) && (count != 5 || 10 || 15 || 20))
    TextIO.putln ("CORRECT!! type....")

    What the compiler was trying to tell you about this is that Boolean OR (|| in Java) can only be applied to statements that evaluate to 'true' or 'false'.

    count != 5

    is such a statement - evaluating it will always result in true or false.

    These on the other hand:
    10 || 15 || 20

    are straight integers and cannot be evaluated as Boolean values (does the number 5 represent true or false? We can't tell!). If you're going to do it this way, you need to explicitly make the comparison in every OR option.

    if (thing == 1 || (thing - 2) == 5 || thing == -34)

    See the difference? All parts of the compound OR statement can be individually evaluated, and thus the whole statement can.

    With regard to the other obvious option, you want the if to pick up any of a number of possible options, but if it finds none of them fall back to a default. You've already used one else statement in this code, it can be applied just as easily to this problem. If you take this route, though, you need to remove the generic statement from everywhere but the else statement or it'll get output multiple times.

My Information

Member Title:
New D.I.C Head
Age Unknown
Birthday Unknown

Contact Information



Maynia hasn't added any friends yet.


Maynia has no profile comments yet. Why not say hello?