Accessing static variable from subclass
Hi,
this question is probably fairly common but I can't seem to find the answer around: Can somebody please explain the rationale behind the following behavior ?
public abstract class SuperClass {
static String mess;
public class SubClass extends SuperClass {
static {
mess = "Hello world!";
static String getMess() {
return mess;
public class mymain {
public static final void main(String[] args) {
System.out.println(SubClass.getMess());
}gives "Hello world!" as expected whereas
public abstract class SuperClass {
static String mess;
static String getMess() {
return mess;
public class SubClass extends SuperClass {
static {
mess = "Hello world!";
public class mymain {
public static final void main(String[] args) {
System.out.println(SubClass.getMess());
}gives "null". It looks like the initialization block is not executed. Why?
Thanks for your insight,
Chris
>
You're essentially claiming you need to override some static methods.No, there is indeed misunderstanding here. What I need to do is implement the methods with the signature given, I'm not overriding existing methods, in fact I'm not even deriving from any existing class. I only have to create the entry points in my code as defined, then publish them to the DB, and Oracle is going to use them (I think they can be called callbacks, also again not 100% sure).
Then it happens that in my particular case it's natural to have a master containing all the code and then subclasses that only define a few specific parameters that are to be used by the static (and instance) methods. Hence the final design. Currently my code looks like the following and seems to work (fingers crossed):
class ParseFileCLL extends ParseFile {
// Name of the row type.
private final static String rowType = "CLLROW";
// Here I initialize static fields of the ParseFile master class.
static {
fileType = "CLL";
fileStruct = new FileStruct(34);
// Type methods implementing ODCITable interface.
static public BigDecimal ODCITablePrepare(STRUCT[] sctx, STRUCT tfinfo, String sysName)
throws SQLException {
// prepareContext is a static helper method defined in the master class.
return prepareContext(funcType, rowType, rowSetType, tfinfo);
// Other ODCI methods are only accessed directly in the master class, NOT in the subclass. Or else... WEIRD BUGS!
// In other words:
// publish ParseFile.ODCITableStart() -> ok
// publish ParseFileCLL.ODCITableStart() -> crash
}Not surprising. Java has plenty of undefined or inconsistently-defined behavior. The JLS is by no means perfect.
>
Well I kind of admire your composure about this, but it seems to me that if it's indeed the case, the meaning of it would be that the code could work in JVM 1.5.0.15 and not in 1.5.0.16, or worse run ok on Windows and not on Linux, which is if I understand correctly precisely the kind of behavior that Java was meant to cure, at least at its inception.
I think there might be other elements to the story though.
Thanks,
Chris
Similar Messages
-
How to access static variable from a Thread class
Kindly help me.......
here's the code.....
class Thread1 extends Thread
int j=0;
myClass2 mc = new myClass2();
public void run()
for( int a=0;a<6;a++)
{try
{ Thread.sleep(5000);
catch(Exception e){System.out.println("Interrupted Exception");}
j++;
mc.change1(i);
} System.out.println("Thread1 executes "+j+" times");
class Thread2 extends Thread
int k=0;
myClass2 mc1 = new myClass2();
public void run()
for( int a=0;a<6;a++)
{try
{ Thread.sleep(5000);
catch(Exception e){System.out.println("Interrupted Exception");}
k++;
mc1.change2(i);
}System.out.println("Thread2 executes "+k+" times");
class myClass2
static int i=5;
public synchronized void change1(int s)
s=6;
System.out.println("New value of i:"+s);
public synchronized void change2(int s)
s=7;
System.out.println("New value of i:"+s);
public static void main(String args[])
Thread1 b1 = new Thread1();
Thread2 b2 = new Thread2();
b1.start();
b2.start();
I am unable to pass the variable i in my method call in Thread1: mc.change1(i); and similarly in Thread2:mc.change2(i);You can declare your i variable in myClass2 as public static and then simply call there
mc.change1( myClass2.i ) ; -
How can I access JSP variables from a JavaScript function in a JSP page?
Respected sir
How can I access JSP variables from a JavaScript function in a JSP page?
ThanxYou may be get some help from the code below.
<%
String str="str";
%>
<script>
function accessVar(){
var varStr='<%=str%>';
alert(varStr);// here will diplay 'str'
</script> -
Accessing public variables from other classes
Probably a simple questions, but how can I access a variable from another class. My exact situation is as follows.
A class called WorldCalender has a variable which is defined: public int hour; (the value is given to it elsewhere).
I want to access this variable and increase it by one in a subroutine in the class Hour. In this class I have put: WorldCalender.hour++; but it doesn't seem to work. How should I do it?don't expose the hour variable at all.
have a method eg addToHourBy( int hrs )
Probably a simple questions, but how can I access a
variable from another class. My exact situation is as
follows.
A class called WorldCalender has a variable which is
defined: public int hour; (the value is given to it
elsewhere).
I want to access this variable and increase it by one
in a subroutine in the class Hour. In this class I
have put: WorldCalender.hour++; but it doesn't seem to
work. How should I do it? -
Non-static variable from static context?
Hi,
I've created a program using swing components
and I've set up a addActionListener to a button,
button.addActionListener(this);
when try and compile I get the following error:
non-static variable this cannot be referenced from a
static context
button.addActionListener(this);
I've checked site and my notes I don't seem to have
done anything different from programs that have compiled
in the past.
I'm currently doing a programming course so I'm fairly
new to Java, try not to get to advanced on me :)
Thx in advance for any help.
ChrisWell what is declared static? If I remeber right this error means that you have a static method that is trying to access data it does not have access to. Static methods cannot access data that is intance data because they do not exist in the instance (not 100% sure about this but I believe it is true, at the very least I know they do not have access to any non-static data). Post some more of your code like where you declare this (like class def) and where you set up the button.
-
Accessing String variables from several JAVA classes
Hi.
I have several java classes that accesses the same String variables. Instead of putting the String declarations in every java files, how can I put these declarations in a single file source, and get each java class to get the variables data from this file ?
Please advice.
Thanks.hi, of course you can solve it by the following methods:
Method 1. define a superclass including the common string variable, and extend other classes from the superclass.
Method 2. define a class , and define your common string variable as a static variable in it. In your other classes, you can call the string variable.
Method 3. define it at your each classes. -
I am not able access static methods from startup class
I have a simple java class which queries the Database and holds some data. I am trying to access a static method of this class from the StartupClass and I am not able to do so. But I am able to access a static variable of the same class.
Can someone help me here.Well, welcome to the world of "hacked" phones. If your purchase was from the US, you obviously purchased an iPhone that is locked to a US carrier. The iPhone must have been jailbroken and unlocked via software to accommodate your foreign SIM card. Now that you have updated the phone, it is locked back to the US carrier. Only the carrier can authorize Apple to unlock the iPhone and none of the US carriers will unlock the iPhones. Best suggestion is to sell the phone and obtain a factory unlocked version directly from the Apple store.
To answer your other question, there is not a supported method to downgrade iOS. -
Accessing static context from instances...
Hello,
I'm confused with something. Accessing static elemnts or static methods from an instance is allowed. I think it shouldn't be allowed. Because static context is class-wide so objects shouldn't be able to access them.
Am I wrong..?I find it confusing.. If something is class-wide then
it should only be accessed by the class, not the
instance of the class...That depends on the nature or purpose of the information. Static information or methods are a useful way of sharing information between instances of the same class, while still having that information protected from other classes. Other examples are constants which are used within each instance. Why should each instance have a separate copy of exactly the same constant?
You could even argue that any method which does not need any instance information should be declared static, but it is not always necessary to make that distinction.
Eclipse (mine at least :) ) gives a warning when you access a static
method/variable 'through' an instance, telling you that you should access >the method/variable in a static way.This warning usually occurs if you call a static method in a non static way. For example, if you declare a class called MyClass with a static method myMethod() and you have an instance assigned to variable anInstance, you should call the method using MyClass.myMethod() instead of anInstance.myMethod().
Graeme -
How to access a variable from within a symbol.
How do I access a variable set outside a symbol from within that symbol?
ThanksIf you set a variable on stage ,say
sym.setVariable("stageVariable", "I am stage variable");
You can access it from within a symbol using :
var myVariable = sym.getComposition().getStage().getVariable("stageVariable");
Basically you need to get handle to the symbol in which the variable is defined. -
Non-static variable from a static context
This is the error i get . If i understand the error correctly it says im using a static variable when i shouldnt be? Or is it the other way round? below the error is the actual code....
The error...
Googler.java:27: non-static variable this cannot be referenced from a static context
submitButton.addActionListener(new ButtonHandler());The code...
JButton submitButton = new JButton("Submit Query");
submitButton.addActionListener(new ButtonHandler());thanks for the response.
I have already tried what you said but I tried it again anyway and i get the same error more less...
Googler.java:28: non-static variable this cannot be referenced from a static context
ButtonHandler buttonHandler = new ButtonHandler();here is part of my code
public class Googler
static JTextField input1, input2;
public static void main(String[] args)
JFrame myFrame = new JFrame("Googler v1.0");
Container c = myFrame.getContentPane();
JLabel lab1 = new JLabel("Enter Google Query:");
JLabel lab2 = new JLabel("Enter Unique API Key:");
input1 = new JTextField(15);
input2 = new JTextField(15);
JRadioButton radSearch = new JRadioButton("Search Query");
JRadioButton radCached = new JRadioButton("Cached Query");
JButton submitButton = new JButton("Submit Query");
ButtonHandler buttonHandler = new ButtonHandler();
submitButton.addActionListener(buttonHandler);
ButtonGroup group = new ButtonGroup();
group.add(radSearch);
group.add(radCached);Ive tried declaring buttonHandler as a static variable and this dosn't work either. I've never had this problem before it must be something silly im missing...?
Thanks
Lee -
Is there any way at all of accessing captivate variables from flash?
Hi,
I would be grateful for any help on this one. I have been working in flash and I wanted to know whether there is any way at all of calling/accessing captivate 4 variables from flash (I'm working in flash Cs4 Actionscript 3.0 and I'm trialling Captivate 4, but I just want to know whether there is any way/method of communicating between a flash file and a captivate 4 file, and if so, can this be done with an embedded captivate 4 file/swf). Surely there must be a way of communicating between the two - they are both Adobe products!
Thanks for any help, pointers, advice, tips in advance.Hi Louon,
Don't know if this would help you.
http://blogs.adobe.com/captivate/2009/09/captivate_variables_-_how_to_a.html
We have many experts here who could give more precise answers.
Cheers.
Miguel -
Access payroll variables from custom payroll function
Hi All,
I have a requirement to create a custom payroll function that returns some data from IT0006 and IT0032.
I have written the code in PCBURZDE0, as the payroll is for Germany, it does read the data needed.
My question is, how I can access the variables specified for the function in the payroll schema.
Schema definition in PE01
Zeile Fkt Par1 Par2 Par3 Par4 D Text
000010 BLOCK BEG Fahrradleasing
000020 _DFRD &PRO GEN
000030 BLOCK END
If I double-click Par1 for function _DFRD (&PRO), then I see he two variables I should write the data to:
Zeile VarArg. FZ T Operation Operation Operation Operation Operation Operation *
-------------+---------+------------------+---------+---------+---------+---------+
000010 AMT= PKWWRPRINT *
000020 1 AMT= ENTKM PRINT
So, as I understand I should write the two variables PKWWR and ENTKM, I have the values in the function code, but I don't know how to access these variables.
Thanks for your help!
Gyöngyi1 - PIT is a Function that is used in the Payroll Schema to "Process the Internal Table".
2 - You mentioned that you had created a Custom Function to read IT0006 and IT0032, and I only suggested to you that you could have that Custom Function generate Information WageTypes and save them in the Input Table where they could then be used in PCRs.
3 - I did not mention that you can read only one value through operation AMT, but I mentioned that using AMT=xxxxxx followed by AMT=yyyyyy means that the first statement is useless. On the other hand, using AMT=xxxxxx followed by AMT+yyyyyy and AMT+zzzzzz will result in the amount being the total of the 3 values.
4 - Please read some documents on Schemas, Functions and PCRs, such as in the Wikis on this site, and study your current Payroll Schema, it's sub-schemas, it's functions and the operations in it's PCRs. Note that t-code pe01, pe02 and pe04 can be used to find some documentation on functions and operations. -
Why Inner class cannot access static variables
Why is it that inner class can use only static final variables of the outerclass, and not ordinary static variables of the outer class. "Yes the JLS sepcifies that only final static variables can be used inside an inner class, esp a non blank final variable". But why this restriction.
Thanks.so what are final static variables treated as if they
are not variables. So if the final static value is
not loaded when the class is loaded how will the
class know about the value.??The actual value wil be substituted for the name of a static final value at compile time. That's why you can use them in switch statements where you can't use any variable variable.
This is something to watch out for, by the way, because if you use a public static final value from one class in another the actual value will be compiled into the using class, so if you change the value where it's defined the class using it will have the old value until it's recompiled. -
Accessing another variable from another application
I was wonder how I can access another variable form another application?
For example: I want to access :P4_EMPLOYEE_NAME from app ID 104 when I am doing is from an activity in page 2 on app ID 142.
I had a search before, but I am looking for an unambiguous response.Greg - About all you can do is call the function htmldb_util.fetch_app_item(p_item => 'F104_ITEM', p_app => '104'). The application you call this function in must be sharing a session with the application from which you are fetching the item, both applications must belong to the same workspace, and the item you name in the argument must be defined as an application-level item in the fetched-from application.
Scott -
class Array{
static int m = 10; // how to access this variable in main()?
public static void main(String [] args) {
int m = 45;
System.out.print(m );
return ;
}One problem is you are doing stuff in main that should not be done in main.
Main is always only for kicking a program off and ensuring it cleans up nice when done, that is it. Sooner you figure this out and get into good habits the better.
Second, you have two m variables, which one do expect to get accessed? But still, getting out of main before doing your computations will make your problem much easier to see and fix, so do that first.
JSG
Maybe you are looking for
-
In module pool program dump due to ''too many consecutive nested call scree
i got a dump i found that this is due too many consecutive nested call screens ,i have used call screen statement in program ,in program once the transaction is completed successfully ,on pressing the ok button ,it will go to the first screen , here
-
Multiple LABVIEW GPIB Commanding alternating Write followed by read many times
Hi, I am trying to convert my labwindows C program over to Labview. This is a VISA GPIB application. I have seen how you can send multiple commands in Labview by sending the commands separated by semicolons. However with my instrument. I need to
-
Register a product purchased in the USA in another country
Can I register a CS6 Suite purchased in the USA in my son's computer in México?
-
The problem I have is as follows: I try to make DVD containing let's say 10 short films. All of them are *.avi PAL 720x576 pixels - DV compression. They are made in Adobe Premiere Pro under Windows, but this itself shouldn't be the problem. My projec
-
What is the hex for the font colors in iOS Pages?
What is the hexadecimal for the font colors that can be used in iOS Pages (specifically, the hex for the four shades of yellow)?