• Post Reply Bookmark Topic Watch Topic
  • New Topic

statics

 
rob armstrong
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok i have a program that reads a few files and will eventullaly use that data to make a new file. Now I understand reading databases/files are done in static methods. So the one file i read has about 10 lines of info than a spacebar (.txt file) than about 10 lines of info which i want to create as a OBJECT itself. than repeatedly a space and 10 lines to be made as ANOTHER OBJECT. There could be as many as 150 objects or so depending on the file it is reading. My problem is making objects in a static context. Thanks for any help
roba
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now I understand reading databases/files are done in static methods.
.

Why do you think so? I do not see any problem with reading a file in a non-static context. Do you?

My problem is making objects in a static context.
.

Why is that problem to you, there is no restriction in the Java platform to create objects in a static context. It is as simple as using the new operator.
[ September 27, 2006: Message edited by: Edwin Dalorzo ]
 
rob armstrong
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ya but calling it from the main method is causing problems.

This line is in the main method (static)
These class's are not static

NewCarEquipment nCarEquip=robReader(bf2);

roba
 
Joe Ess
Bartender
Posts: 9361
11
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"robReader(bf2)" is a method invocation, not a class. And yes, you cannot invoke a non-static method from a static one.
The Java Tutorial: Understanding Instance And Class Members
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, first the first. You cannot call an instance method of the same class within the static context of the class. For instance this is not possible:



You cannot call an instance method of the same class within the context of the static method because the instance method must belong, logically, to a particular object, you cannot tell on what instance you are calling the method sayHello() within the static method because no instance of Ranch exists yet. Think about it.

But you could create an instance of your class, and use it. Like this:




Does that help?
 
rob armstrong
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yep helps alot. Just having trouble wrapping my head around statics.
roba
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!