Solved

how to access a class eveywhere

Posted on 2000-05-09
3
154 Views
Last Modified: 2010-04-01
Can anyone tell me what I am doing wrong
say if I do this
 
 
        try{
                RandomAccessFile fxxx = new RandomAccessFile("test", "rw");
        }
        catch(Excption e){
                System.out.println("Error: " + e.toString());
        }
 
and later on try
 
fxxx.write(buf);
 
It will tell me fxxx is a unknow class, If
I try it right after I create it I can acess it
So what I want to know is how to acess fxxx everwhere in the program
 
Thanks for any help.
0
Comment
Question by:thedogeater
  • 2
3 Comments
 
LVL 16

Accepted Solution

by:
imladris earned 50 total points
Comment Utility
The variable fxxx will need to both persist and gain wider scope.

First persistence. As it is declared the variable fxxx will go out of scope, which will cause it to cease to exist, as soon as execution leaves the try block. To "survive" beyond its current block or method, a variable must be either be an "instance" variable or a "class" variable. If a variable is an "instance" variable a distinct copy of it exists in each "instance" of the class. It would be declared:

class FileAcc
{   RandomAccessFile fxxx;

    try
    {   fxxx=new RandomAccessFile("test","rs");
    ...
    ...

If it is a "class" variable, one copy of it exists regardless of how many instances of the class exist (or any at all). In this case you declare it as:

class FileAcc
{   static RandomAccessFile fxxx;

    try
    {   fxxx=new RandomAccessFile("test","rw");
    ...
    ...

An instance variable is accessed with a reference to the specific class instance:

class someother
{   FileAcc fa=new FileAcc();

    fa.fxxx  // is the reference here

For a class variable you can access it with:

    FileAcc.fxxx

To make it accessible in other packages you will have to add the "public" keyword to the declaration, and possibly import the package "FileAcc" is in. If you do everything in the default package this need not concern you.
0
 
LVL 16

Expert Comment

by:imladris
Comment Utility
Is there any further clarification needed before you grade the answer?
0
 

Author Comment

by:thedogeater
Comment Utility
Thanks a lot
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now