aspose file tools*
The moose likes Beginning Java and the fly likes using new in the main method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "using new in the main method" Watch "using new in the main method" New topic
Author

using new in the main method

Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
I was writing a simple class with main as the only method and needed to populate a static array of objects. I got the error that "this" can't be used in a static context.
Am I correct that user-defined objects can't be created using the new operator in a static method because new calls a constructor and constuctors are passed the this reference?
This would explain why I could create strings and arrays - they don't have constructors.
For some reason, none of my manuals mention this rule.
I encountered this issue in Exercise 1.10 of "The Java Programming Language, Third Edition".


Mike Gershman
SCJP 1.4, SCWCD in process
Davy Kelly
Ranch Hand

Joined: Jan 12, 2004
Posts: 384
this compiles and runs:

in a constructor the keyword super or this can be used, which ever is used must be the first statement in the code block. But if you forget to include any of these, the constructor implicitly includes the super keyword for you.
Davy
[ March 13, 2004: Message edited by: Davy Kelly ]

How simple does it have to be???
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
How about this code:
class Foo8
{
class Foo9 {
int x;
}
static Foo8 f8;
static Foo9 f9;
public static void main(String [] args)
{
Foo8 f8 = new Foo8();
Foo9 f9 = new Foo9();
}
}
Notice how I can instantiate my own class, Foo8, but not the internal class Foo9.
Carlos Failde
Ranch Hand

Joined: Oct 20, 2000
Posts: 84
To create an instance of a non static nested class (an inner class) you need an instance of the outer class
Foo9 f9 = f8.new Foo9();
OR
Foo8.Foo9 anotherf9 = new Foo8().new Foo9();
You can find help with Getting in Touch with your Inner Class
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
I was writing a simple class with main as the only method and needed to populate a static array of objects. I got the error that "this" can't be used in a static context.
Right. You can't use the keyword "this" in a static context. If you'd like a bit more nudging on how to fix your code, do feel free to post it.
When posting code, please be sure to surround the code with the [code] and [/code] UBB Tags. This will help to preserve the formatting of the code, thus making it easier to read and understand.
Am I correct that user-defined objects can't be created using the new operator in a static method because new calls a constructor and constuctors are passed the this reference?
No. Code such as MyClass instance = new MyClass(); works just fine in a static context (provided the proper class definition is available).
Constructors are not passed the this reference. (That is, unless you've written explicit code that passes a reference to this to a constructor.)
This would explain why I could create strings and arrays - they don't have constructors.
Wrong. Constructors are indeed defined in the String class. Take a look at the J2SE API documentation for the String class documentation.
For some reason, none of my manuals mention this rule.
Well, hopefully it's now clear that such rules, as those you'd assumed existed, actually don't exist. So, your manuals don't mention them.
You're struggling with the concepts involved with understanding instance vs. static contexts. And it's good that you're thinking and asking about them. These are things you'll need to know well to be a good (or even adequate) programmer.
Some concepts you'll want to understand include the following.
  • The keyword this refers to the current instance (object) of the class.
  • A class is an object data type definition.
  • If something in a class is defined to be static, then it "belongs" to the class, and there's only one of them.
  • If something is defined to not be static, then it belongs to instances (objects) of the class, and each instance has its own copy of the thing.

  • [ March 14, 2004: Message edited by: Dirk Schreckmann ]

    [How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
     
    wood burning stoves
     
    subject: using new in the main method