The Artima Developer Community
Sponsored Link

Java Answers Forum
objects in java

2 replies on 1 page. Most recent reply: Oct 25, 2004 2:20 AM by Matthias Neumair

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
fa

Posts: 1
Nickname: fsharmarke
Registered: Oct, 2004

objects in java Posted: Oct 21, 2004 11:01 PM
Reply to this message Reply
Advertisement
Hi, every1, is there a way around to make an object from like:
class myclass{
static void(some parameters)
...do stuff
}
public static main method(....){
...do stuff
},

without creating new class for that purpose.
thanx in advance.


Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: objects in java Posted: Oct 25, 2004 2:13 AM
Reply to this message Reply
I don't understand exactly what you want.

A object is a instace of a class, that's the definition.


If you don't want to create own classes, you can't code anything.

Your example was a executable class, but normally you don't need the main-Method.

static void(some parameters) doesn't make any sense at all.Y ou want to create a constructor? Then you should write
public myclass (some parameters) {}

btw: class names and the constructor should be upper case (Myclass), everything else lower case.

If you used BASIC until now: Yes, it IS possible to write spaghetti code in Java, there is way to create classes in the code, but it should be used rarely if there is no other way.

I'm writing on a medium size project, until now I created areound 310 classes.

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: objects in java Posted: Oct 25, 2004 2:20 AM
Reply to this message Reply
Ok, now I understood what this static void was about.

You made it static because you want to call it from the main method.

Static methods can only access other static methods, objects or variables.

Normally, the main method would not contain a lot of code.

If you want to use only one class, the standard solution is the following:

public MyClass {
public MyClass (parameters) {
//now, that you have an instance of this class, you don't need static methods anymore
myMethod1(parameters);
}
private void myMethod1 (parameters) {}

public static void main (String[] args) {
//static, because the object does not yet exist when the main method is called by the virtual machine
MyClass myClass = new MyClass(parameters); //let MyClass do the work
System.exit(0);
}

Flat View: This topic has 2 replies on 1 page
Topic: New Project needed in java-j2ee Previous Topic   Next Topic Topic: help for creating a linklist

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use