• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Quick Question

 
Kimo Sogi
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Notice that you are missing something in the declaration.
 
vignesh hariharan
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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..
 
Kimo Sogi
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Kimo Sogi
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9065
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Kimo Sogi
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Kimo Sogi
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Wow never thought about doing it that way. Thanks!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic