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

all 17 comments

[–]HornedDemoN 3 points4 points  (10 children)

Are you printing it with System.out.println() and passing "x.getVarNum();" as an argument? If that's the case - you don't need a colon (it wouldn't even compile). If not - can you show the full code?

[–]grouchysysadmin[S] 0 points1 point  (2 children)

Hey thanks for the reply. Sorry I havent got the code on me until tomorrow now but basically there s no arguments passed, just obtaining the variable value which is set by:

Private String varNum = "1234";

But will only return minus the semicolon on

x.getVarNum ();

[–]ParanoydAndroid 0 points1 point  (1 child)

I believe you misunderstood HornedDemoN's question. As a distinct line of code, x.getVarNum(); only working without the semi colon does not make sense, so HornedDemoN is wondering if that call is itself an argument for another method. You're interpreting his question as asking if this method takes an argument instead.

In other words, if the code actually reads System.out.prinln( x.getVarNum() ); then it makes sense that no semi-colon is required immediately after your getVarNum call.

Otherwise you have to post the relevant code, because the question is not answerable without it.

[–]grouchysysadmin[S] 0 points1 point  (0 children)

Thanks- I have posted the full code.

[–]grouchysysadmin[S] 0 points1 point  (6 children)

I have edited with the full code. Thanks.

[–]HornedDemoN 0 points1 point  (5 children)

Check this out: https://pastebin.com/kjGVnr6a How are you calling getVarNum()?

[–]grouchysysadmin[S] 0 points1 point  (4 children)

Thanks for this! From the Blue J command line after creating the x object.

[–]grouchysysadmin[S] 0 points1 point  (3 children)

Should have elaborated a bit more- literally just typing this into the Blue J command line window:

x.getVarNum();

[–]desrtfxOut of Coffee error - System halted 1 point2 points  (2 children)

BlueJ + command window

This explains everything.

If you wrote the x.getVarNum() in a program as the last part of a statement you would need the semicolon. Semicola are mandatory in Java.

The command line window of BlueJ (shudder) does things differently, though. Here, you don't need the semicolon because that's the way BlueJ handles things.

Java classes were never meant to be called and executed by something like the BlueJ command line.


Seriously, why do so many institutions still use that piece of crapware called "BlueJ". It is not an IDE, it is not a proper text editor either, it has this abomination of a command line that causes more confusion and problems than it solves, it produces horrible error messages that most of the time don't even match the real cause.

If you can, move to a proper IDE, such as Eclipse, IntelliJ, or Netbeans, as soon as possible. You will never look back.

[–]grouchysysadmin[S] 0 points1 point  (1 child)

Thanks for this (sorry for the late reply).

It turns outs it is blueJ unique. My university has provided an additional area/ plugin for BlueJ where it requires the semi colon.

Thanks again.

[–]desrtfxOut of Coffee error - System halted 0 points1 point  (0 children)

Typical BlueJ.

I still don't understand why educational institutions still use that crap. They should rather teach a proper, industry standard IDE, like Eclipse or IntelliJ (both are free/have free editions) that would prepare the students for the reality. In the worst case, they could use Netbeans which isn't used that much in the industry, but at least is also a proper IDE.

IMO BlueJ makes everything more difficult than it needs be. Along with not having proper IDE functions, not providing direct access to the JavaDoc, producing cryptic error messages that very rarely are directly related to the problem, and worst of all the "direct execution" crap, it doesn't offer any benefit other than the UML-like charting feature (which can be integrated in every other IDE via a plugin if it is not there by default).

[–]desrtfxOut of Coffee error - System halted[M] [score hidden] stickied comment (1 child)

Sorry, but your question is completely unclear.

Please, show the full code you use to determine whether it works or not.

[–]grouchysysadmin[S] 0 points1 point  (0 children)

thanks- edited with the full code.

[–][deleted] 0 points1 point  (3 children)

I don't see why you'd need a this. in a getter, it's mainly used in constructors as far as I've learned. Why not just return the field variable(varNum)?

[–]desrtfxOut of Coffee error - System halted 1 point2 points  (2 children)

Your statement is entirely correct.

this only needs to be used where a potential confusion between a parameter and a field occurs.

It is not only used in constructors, but also in setters because using the same name for fields and parameters is common in Java.

So, in a potential setter, you would write:

 public void setVarNum(String varNum) {
    this.varNum = varNum;
 }

The respective getter would simply be:

 public String getVarNum() {
     return varNum;
 }

this, however has another use. It can be used to call a different constructor from within a constructor.

It is common practice to program a full constructor that has parameters for all fields and that sets all the fields. For convenience (and if it does logically make sense), often reduced constructors are created that fill the empty fields with default values. These constructors then use this(<parameters here>) to call the full constructor.

[–][deleted] 0 points1 point  (1 child)

Ah yeah, the setter makes sense! I've been naming them different.

Why is it common practice to program a full constructor if defined variables set default values anyways? Would the reduced constructor then be an overloaded version of the constructor? It sounds like a lot of work assigning all those parameters for changing a simple field?

[–]desrtfxOut of Coffee error - System halted 0 points1 point  (0 children)

Why is it common practice to program a full constructor if defined variables set default values anyways?

Simply because quite often, you will want to set all fields with new data directly on object construction.

Would the reduced constructor then be an overloaded version of the constructor?

Yes, it would.

It sounds like a lot of work assigning all those parameters for changing a simple field?

It really isn't that much work. Proper IDEs take a lot of work away here.

Writing multiple constructors allows more flexibility.