r/javaTIL • u/macarthurpark431 • Apr 08 '15
TIL: You don't actually need to have separate lines in your code; an entire class can be written in only one line.
I just found this out the other day. Doesn't seem very useful, but it is entertaining. Example:
public class Test{public static void main(String[] args){System.out.println("hello"); System.out.println("goodbye");}}is the same as
public class Test
{
public static void main(String[] args)
{
System.out.println("hello");
System.out.println("goodbye");
}
}
0
Upvotes
10
u/Stromovik Apr 09 '15
yes you can , but why would you ?
6
u/dartalley Apr 09 '15
Don't forget the existence of inner classes. If you really wanted you can write multiple classes all one on line.
4
u/wnz Apr 09 '15
You don't have to do the compiler's job ;-)
2
u/honestduane May 02 '15
..And if anybody on my team actually checked in code like this, I would seriously consider firing them.
2
u/squishles Jun 02 '15
next is realizing you can write things like this.
I don't know why doing that isn't the thing :(
1
u/[deleted] Apr 08 '15
I'll give you three days to prove your theory correct. After that... we'll have to hunt down Ramius (the one-line coder) and destroy him. Will you do it?
1
u/desrtfx Apr 09 '15
Java is not C. You just don't pack everything in one line. Just like you don't pack a whole book in a single paragraph.
There are code conventions for Java that also regulate the use of white-space (blanks, empty lines, paragraphs, etc.)
Learning these code conventions at the earliest possible stage is by far more beneficial than finding out that code can be written in a single line.
0
u/demigodjessica Apr 09 '15
Not a good habit to have if you're ever going to learn a white space reliant type language like Python.