• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Quick Question

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ();
}

}
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Notice that you are missing something in the declaration.
 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ();
}
}
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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