• Post Reply Bookmark Topic Watch Topic
  • New Topic

why I can't print from inside a constructor ?  RSS feed

 
Dan D'amico
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Tim Harris
Ranch Hand
Posts: 57
3
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you be specific as to what kinds of errors you are getting right now?
 
Dan D'amico
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Harris wrote:Can you be specific as to what kinds of errors you are getting right now?


I'm not getting errors .
but I thought that when I compile it, it will print "print" . and it doesn't.
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It won't print when you compile it, it will print when you run it.
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your code sample is one closing curly brace short by the way.
 
Tim Harris
Ranch Hand
Posts: 57
3
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dan D'amico wrote:
I'm not getting errors .
but I thought that when I compile it, it will print "print" . and it doesn't.


Compiling simply turns your code into code that your machine can understand. As Mike has stated, you need to run the compiled code and then see what happens.
 
Dan D'amico
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Harris wrote:
Dan D'amico wrote:
I'm not getting errors .
but I thought that when I compile it, it will print "print" . and it doesn't.


Compiling simply turns your code into code that your machine can understand. As Mike has stated, you need to run the compiled code and then see what happens.


compiling is ok . when I run it. it dont print "print" . and it should. I dont understand why.
 
Tim Harris
Ranch Hand
Posts: 57
3
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dan D'amico wrote:
compiling is ok . when I run it. it dont print "print" . and it should. I dont understand why.


This may be a silly question, but how are you running it? From the command line? IDE? Some other method?
 
Dan D'amico
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Harris wrote:
Dan D'amico wrote:
compiling is ok . when I run it. it dont print "print" . and it should. I dont understand why.


This may be a silly question, but how are you running it? From the command line? IDE? Some other method?


CMD
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dan D'amico wrote:
CMD


And how are you running it from the command line?

Henry
 
Dan D'amico
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Dan D'amico wrote:
CMD


And how are you running it from the command line?

Henry


javac Humen.java
javac B.java

java B
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code you posted has a class Human but in your latest post you say you are compiling Humen.java. Is that a typo here, or do you have another class named Humen?
 
Dan D'amico
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Darryl Burke wrote:The code you posted has a class Human but in your latest post you say you are compiling Humen.java. Is that a typo here, or do you have another class named Humen?


its a typo here.

I meant human.

there's something wrong with my compiler . the code is legit.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I took your code as posted (apart from the missing close bracket in the Human class), saved it as two files, and compiles/ran it. here's my output:

C:\slop>javac Human.java
C:\slop>javac B.java
C:\slop>java B
print
C:\slop>

it is highly doubtful it is the compiler. what do you get when you do:
javac -version

?
 
Puspender Tanwar
Ranch Hand
Posts: 499
2
Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if you are using version 1.8, your code would not compile, as in java8 you cannot have a multiple public class. You can have only one public class, and that must be the one you have saved as your file name
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is not just a Java 8 rule, it has always been the case as far as I know.
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am assuming you are having 2.java files
1-Human.java
2-B.java

First thing apart from your question, you don't need to compile Human.java then B.java
Just Compile B.java(your Human.java)will be compiled automatically.

Now talking about your code ,I just can't see any reason that it won't print- (print),if it compiles successfully.
 
Dan D'amico
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:I took your code as posted (apart from the missing close bracket in the Human class), saved it as two files, and compiles/ran it. here's my output:

C:\slop>javac Human.java
C:\slop>javac B.java
C:\slop>java B
print
C:\slop>

it is highly doubtful it is the compiler. what do you get when you do:
javac -version

?


1.8.0_25
 
Dan D'amico
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes I wrote the classes in different notepad files and it worked.

thanks all for the help
 
Campbell Ritchie
Marshal
Posts: 56587
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stop using Notepad. It is not helpful for programming. We have an FAQ about editors, and I have my own suggestionslink. Read the 2nd and 3rd links in this link←same link. If I have the misfortune to have to use Windows, my favourite editor is Notepad++.
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dan D'amico wrote:yes I wrote the classes in different notepad files and it worked.
thanks all for the help

And this is a classical example, how important is to TellTheDetails (<- Link. Please have a look, you'll find some useful tips).

Earlier you mentioned:
Dan D'amico wrote:javac Human.java
javac B.java

java B

So everyone assumed you had these two classes in separate files, it seemed not. Moreover, you didn't tell us before you had errors when you compiled your source file.

So, short summary related to your problem about outer classes:
1. You cannot have more than 1 public class within single ".java" file.
2. You can have more than 1 class defined in a single ".java" file, once again, only 1 can be "public", others must be with "default" access modifier, it means - no access modifier specified. Look what does "default" mean in terms of class visibility.
3. If you do have defined class with "public" access modifier, then "public" class name has to match with file name. Case sensitivity is important.
4. If source file doesn't contain "public" classes, then file name and class name/-s no need to match.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!