aspose file tools*
The moose likes Beginning Java and the fly likes Quick Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Quick Question" Watch "Quick Question" New topic
Author

Quick Question

Kimo Sogi
Greenhorn

Joined: May 25, 2006
Posts: 12
Why cant i get the second method Hello1 in the code below to compile?

/////////////////////////////////////////////

class Hello {

public Hello () {
String x = "Hello World";
System.out.println (x);
}

public Hello1 () {
String y = "Hello World";
System.out.println (y);
}

public static void main (String[] args) {
new Hello ();
new Hello1 ();
}

}
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Notice that you are missing something in the declaration.
vignesh hariharan
Ranch Hand

Joined: Jun 23, 2005
Posts: 77
friend...

a method must have a return type.. and a constructor is jus opposite to it.. it should not have a return type.. and the call for a method is incorrect..


Regards,
vignesh
Kimo Sogi
Greenhorn

Joined: May 25, 2006
Posts: 12
Thanks for the advice. This is the new code I came up with to solve the problem. Tell if this is right or if there is a another way.

/////////////////////////////////////////////

class Hello {
public Hello () {
String x = "Hello World";
System.out.println (x);
}
}

class Hello1 {
public Hello1 () {
String y = "Hello World";
System.out.println (y);
}
}

class MainStart {
public static void main (String[] args) {
new Hello ();
new Hello1 ();
}
}
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

That's right, in the sense that it compiles and does something, but all by itself, it doesn't necessarily make sense. You're defining entire classes Hello and Hello1, when perhaps all you want are two methods hello() and hello1():



Do you understand the difference?


[Jess in Action][AskingGoodQuestions]
Kimo Sogi
Greenhorn

Joined: May 25, 2006
Posts: 12
That makes perfect sense. The only problem is I tried the code you suggested and I get and error in the compiling.

C:\java\MainStart.java:14: non-static method hello() cannot be referenced from a static context
hello();
^
C:\java\MainStart.java:15: non-static method hello1() cannot be referenced from a static context
hello1();

So I changed the two method to static and it compiles but by labeling it static is this proper coding and is there and disadvantages by using static methods? Also thanks for the reply with the above code it explained a lot! stuf
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9047
    
  10
Hello, Kimo Sobi
Welcome to JavaRanch. Aside from the "be nice" rule, we have a naming policy. Please read the JavaRanch naming policy and adjust your display name to something more appropriate (perhaps a variation of your real name).


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Kimo Sogi
Greenhorn

Joined: May 25, 2006
Posts: 12
To funny. That is a variation of my real name. But I wil lchange it to something more appropriate. Sorry for any trouble.
Jaime M. Tovar
Ranch Hand

Joined: Mar 28, 2005
Posts: 133
try this

class MainStart {
public void hello () {
String x = "Hello World";
System.out.println (x);
}

public void hello1 () {
String y = "Hello World";
System.out.println (y);
}


public static void main (String[] args) {
//You need an instance of the object to call non static methods
MainStart mainStart = new MainStart();
mainStart.hello();
mainStart.hello1();
}
}

In fact static methods are not well seen in object oriented programming. But they are valid anyway. Static methods are very valuable when working with methods that do not have access to instance properties. But when you are using instance properties within the method then I recommend to use non static methods.


She will remember your heart when men are fairy tales in books written by rabbits.<br /> As long as there is duct tape... there is also hope.
Kimo Sogi
Greenhorn

Joined: May 25, 2006
Posts: 12


Wow never thought about doing it that way. Thanks!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Quick Question