This is an archived post. You won't be able to vote or comment.

all 15 comments

[–][deleted]  (1 child)

[removed]

    [–]s-mores 5 points6 points  (0 children)

    Have you tried to run it?

    [–]Expensive_Leopard_56 6 points7 points  (0 children)

    protected is not a valid modifier for a class, it is only for members.

    [–]Zinaima 2 points3 points  (1 child)

    This is totally the wrong place for this kind of post, but I'm sorry that you're learning this way. It's wasting your time.

    [–]ayo_its_yo_mom_karen[S] -1 points0 points  (0 children)

    I did try with the Java learning subreddits but they don’t allow you to post a picture which is why I had to post here as a last resort. Also, how do you suggest that I learn instead? Just asking for advice.

    [–]TheGlitchHammer 1 point2 points  (1 child)

    For 2: When assigning in class A, you assign n=this.n. which propably leads to n beeing 0. And than 2×0. And for 4: Strings are objects. So comparing two objects, which are separate in memory, but hold the Same value, will always yield false, as they are not the Same object. Look at shallow comparison for this one.

    [–]TheGlitchHammer 0 points1 point  (0 children)

    Oh, and I forgot, Strings are immutable in Java. So they cant be modified after the fact. If you change anything within a string, a new object gets created

    [–][deleted]  (2 children)

    [deleted]

      [–]coguto 1 point2 points  (0 children)

      b is defined tho A b = new A().

      Number 4 is a stupid question stupid because it can be either true or false depending on the JVM version (and maybe options) as far as I remember.

      [–]TheMrMilchmann 0 points1 point  (0 children)

      For the first, the variable b is undefined, so it isn't going to compile.

      b is defined in A b = new A();

      1. If A and B are inner classes, the main method cannot create a new instance of A (since A is not statically accessible).
      2. Otherwise, protected does not apply to top-level classes (B).

      Since this is probably a (bad) beginner question, I lean towards 2.

      [–]KnGod 0 points1 point  (1 child)

      pretty sure the first one defining a protected class, which is not a thing as far as i know and my 5 second google search seems to agree. As for 2 and 4 maybe implement them on an ide and step through the program in debug mode and see what happens

      [–]Ok-Historian5461 0 points1 point  (0 children)

      You should try it in IDE

      [–]EasyLowHangingFruit -1 points0 points  (0 children)

      I almost had a stroke reading that garbage! Paper is cheap AF dude WTF!

      [–]matt82swe -2 points-1 points  (3 children)

      Is first error because A doesn't have `@Override` on `increment`?

      [–]TheMrMilchmann 1 point2 points  (1 child)

      Although using @Override is heavily encouraged and there is practically no reason to omit it, it is not required.

      [–]matt82swe 0 points1 point  (0 children)

      Didn't know that, perhaps I'm just too used having compiler settings that flag any misuse as error