Difference Between Static Keyword And Transient Keyword
Oct 7, 2014What is the difference between the static keyword and transient keyword?
View RepliesWhat is the difference between the static keyword and transient keyword?
View RepliesI've come across something that i'm not overall sure about regarding the static keyword in Java.I'm making a vertical scrolling game where the player simply shoots enemies and they shoot back as they fall, dropping items if they die such as power ups and coins. I have an enemy called Bat and this is the bullet creation code in the update method:
if(oldPlayerY + 220 > posY && getBulletDelay > 0.90f){
batBullets.add(new Bullet(posX + 10, posY - 10));
getBulletDelay = 0;
}
The method is creating a new bullet object and it then adds that to the arraylist called batBullets, which is simple enough. I then need to access this arraylist in the main game update class so I can render those bullets on the screen, even if the bat dies. I was always taught that you use the static keyword when you need to access something from the class that doesn't require an object. Because of this, I have the following code.
for(Bullet bullet : Bat.batBullets){
bullet.setY(bullet.getY - 5); // Set the bullet to fall
renderMap.getSpriteBatch().draw(bullet.batBullet(), bullet.getX(), bullet.getY()); // render the bullets
}
This seems perfectly fine to me because I need to access the batBullet arraylist and it doesn't make sense to create a new bat object as I already have random spawning in place for them.
Suppose I have
class A {
public void speak() {
System.out.println("I am class A");
}
}
class B extends A{
public void speak() {
System.out.println("I am class B");
}
}
class C extends B{
public void speak() {
System.out.println("I am class C");
}
}
Why this doesn't work while A is a super type of B ?
public static void insertElements(List<? super B> list){
list.add(new A()); //not OK, why?
}
Why java uses the keyword extends when setting the bound of a type parameter(Generic) to an interface. I think using the keyword implements is more intuitive.
public static <T extends Comparable<T>>
why use extends? and not implements.
int countGreaterThan(T[] anArray, T elem) {
int count = 0;
for (T e : anArray)
if (e.compareTo(elem) > 0)
++count;
return count;
}
I know if I want to set multiple bounds I will use extends keyword, and I will concatenate the bounds using & operator.
Is this a design decision to always use extends keyword to set bounds?
I am attaching a document which shows the current state of my registry.What I want to know is if I can [safely] delete the JavaSoft folder with all lower subfolders, then re-install jdk1.6/0_31 which, I am told, is the current version being used here by developers.According to others on the development team (not my team), there COULD be something in the registry that is preventing both the installation of java jdk AND its uninstallation.Since I cannot seem to attach any kind of document.
View Replies View RelatedI need creating a java keyword program that can encipher and decipher a message using the provided keyword.
The keyword is :javbean
I have attached the message text as well...
public class Caesarcipher
{
public static final String ALPHABET = "abcdefghijklmnopqrstuvwxyz";
public static void main(String[] args)
{
String theKey = "JAVBEANDEFGHIJKLMNOPQRSTUVWXYZ";
String text= "message.txt";
[Code] ....
message.txt (763bytes)
The super keyword when used explicitly in a subclass constructor must be the first statement but what about if i have a this(parameters) statements ? As the this one must also be the first statement... Does this means that i can have only one or the other ? What about when the super constructor is not explicit (aka implicit ) , can i use the this( parameters) in the same constructor ?
View Replies View RelatedI've been trying for a while to get my exception output to print in a particular form to System.err.
What I'm looking for as output is
KeywordException: edu.cofc.csci221.KeywordException: **Keyword Not Found**
I'm getting
Keyword Exception: edu.cofc.csci221.KeywordException
at edu.cofc.csci221.CheckLine.checkForInvalidKeyword(CheckLine.java:101)
at edu.cofc.csci221.ReadLogFile.main(ReadLogFile.java:47)
The code:
line = scan.nextLine();
try { check.checkForInvalidSymbols(line); } catch (SymbolException sEx) { System.err.print("Symbol Exception: "); sEx.printStackTrace(); }
try { check.checkForInvalidKeyword(line); }catch (KeywordException kEx) { System.err.print("Keyword Exception: "); kEx.printStackTrace(); }
if(check.checkFirstKeyword(line) && line.split(" ")[0].equals(keywords[0])) { System.out.println(line); }
[Code] ....
I'm just unsure of where to go/what to do.
i need to write a method, that passes in an arraylist and a keyword,and display the name of all the people in the arrayList whose name contain the keyword (irrespective of uppercase or lowercase). how to write such a method ??
View Replies View Related//constructor
class Base
{
Base(int a) {
System.out.println("in base"+a);;
}
}
class Cons extends Base
[Code] .....
How does the keyword this in the CoffeeSize class refer to the size of the coffee ? I am also confused as to how the CoffeeSize constructor comes into play to determine the cost.
public class Test
{
public static void orderCoffee(CoffeeSize size)
{
size.print();
}
public static void main(String[] args)
{
orderCoffee(CoffeeSize.SMALL);
}
[code]....
I have a simple classes here one is interface and another one is abstract class when i try to compile them abstract class is givving compilation error.
public interface MyInterface{
public void getName();
public void getName(String s);
}
public class HelloWorld{}
abstract class SampleClass{
[code]....
I am trying to test the instanceof keyword. To do this, I've made a method with a simple logical test like so:
Java Code:
Vehicle vehicle1 = new Vehicle();
public void Type(){
if (vehicle1 instanceof Vehicle) {
System.out.println("Type = Vehicle");
}
else if (vehicle1 instanceof Car) {
System.out.println("Type = Car");
}
else if (vehicle1 instanceof Truck) {
System.out.println("Type = Truck");
}
}
} mh_sh_highlight_all('java');
I wanted to try implementing it into the class definitions for Vehicle, then extend that to Car and Truck, but I'm not sure how to use this test in a general case.
The only way this method works is if I set the test to specifically accept a specific object as a parameter.
I want to test multiple objects, but I'm not really sure how else to do this without simply copy-pasting the logical test multiple times and changing the respective objects that are used as parameters.
I need to search a txt file for a specific keyword and then output all the lines that contain that keyword. Right now I I think I have my search done but I don't know how I would print the whole line.
TextIO.readFile("xxx.txt");
String search;
String word;
int count=0;
TextIO.put("Please enter your search word: ");
search = TextIO.getln();
while (!TextIO.eof()) {
word = TextIO.getln();
count = count+1;
if (search.equalsIgnoreCase(word)==true){
TextIO.put(count + "-");
TextIO.put(word);
Right now it doesnt even let me enter in any values for the search. Not sure what I've done wrong..
I have this very annoying issue with Eclipse (I have the latest version installed). For some reason, every time I use the "default" keyword in an interface, it gives me an error similar to "Syntax error on token default", I deleted the "default" keyword, the error is gone. The same thing happens with "Lambda expression as well", say I have this object like this :
Actions myActions = () -> {System.out.print("Blah blah blah");}; ,
Eclipse also displays the error message similar to "Method body expected after (), delete '->' ". I checked the Java version I have, it is the latest one also ....
I'm not really sure I understand the functional difference between a static and final variable/field. Oracle defines Class Variable as:
Class Variables (Static Fields) A class variable is any field declared with the static modifier; this tells the compiler that there is exactly one copy of this variable in existence, regardless of how many times the class has been instantiated. A field defining the number of gears for a particular kind of bicycle could be marked as static since conceptually the same number of gears will apply to all instances. The code static int numGears = 6; would create such a static field. Additionally, the keyword final could be added to indicate that the number of gears will never change.
If static will have the same value regardless of how many times it's used, then why use final (or vice versa)?
I come from a C++ background and I recently started learning Java from "Thinking in Java" 4th Edition by Bruce Eckel.What's the difference between:
// explicit in class initialization
// saw this on page 126
class A {
}
public class B {
A obj1 = new A();
A obj2 = new A();
}
and
// non static instance initialization
// saw this on page 132
class A {
}
public class B {
A obj1;
A obj2;
{
obj1 = new A();
obj2 = new A();
}
}
This is my code inside the method:
@Post
public static String getDetails(Representation entity) throws Exception {
String customerId = getQuery().getValues("cus_id");
}
I use this code in Restlet Representation. I try to get the value from the Request API. But I am facing the problem as "Cannot make a static reference to the non-static method getQuery() from the type Resource".
From what i understand static methods should be called without creating an instance of the same class . If so why would they return an instance of the same class like in the following : public static Location locateLargest(double[][] a) , the Location class being the same class where the method is defined . I don't understand this , does it mean that every field and every method in the class must be static ? Meaning that you cannot have instances of the class because everything is static . Or it's just a mistake and the class Location cannot have a static method: public static Location locateLargest(double[][] a) ?
View Replies View RelatedI can't figure out what this error message "Cannot make a static reference to the non-static method getEndUserCharge(long, long, long, long) from the type UpdateUserWS" actually means.
The error is coming from:
public void updateDetailsPackage() {
some unrelated code
long zero=0;
double endUserCharge=0;
endUserCharge = UpdateUserWS.getEndUserCharge(long zero, long zero, long zero, long zero); <-------- error is here
[Code] ....
Alright, I have two classes, this one
public class Player {
private String player;
public String getPlayer() {
return player;
}
private int strength;
private int defense;
[Code] .....
However, it says that under Player.getPlayer() that it 'Cannot make a static reference to the non-static method'.
Why I can create an Instance of a class that contains non static variables within the static main method ?
This program runs fine
import java.util.*;
public class Test{
public int j;
public static void main(String[] args) {
Test test1=new Test();
System.out.println(test1.j);
[Code] .....
I am trying to call an actionListener which is shown below in my PSVM :
class testMenuItemListener implements ActionListener {
public void actionPerformed(ActionEvent arg0) {
getContentPane().removeAll();
createPanel();
getContentPane().add(panel1); //Adding to content pane, not to Frame
repaint();
printAll(getGraphics()); //Extort print all content
[Code] .....
I get the following error :
Frame.java:409: error: non-static variable this cannot be referenced from a static context
menuItem1.addActionListener(new testMenuItemListener());
I am trying to add a field (called special) to a hibernate table. I am copying existing code (related to the NAME field) so I don't have to figure this out from scratch. I am getting the error
"[ERROR] C:VOXvoxware-1.1.13voxwarevoxware-implsrcmainjavacomvoxwareimplflowVoxFlowConfiguration.java:[213,38] error: non-static variable special cannot be referenced from a static context".
Line 213 is in public void mergeFrom, the actual line is "special = VoxFlowConfiguration.special;" I don't understand why Java thinks special is a "non-static" variable but it doesn't have a problem with the other variables (such as name, orderShow)
package com.voxware.impl.flow;
import com.voxware.asset.LiabilityType;
import com.voxware.flow.FlowConfiguration;
import com.voxware.flow.OrderFlow;
import com.voxware.flow.Step;
import com.voxware.i18n.LanguageCodes;
import com.voxware.impl.i18n.UTF8Control;
import com.voxware.impl.persistence.BaseEntity;
import com.voxware.impl.portal.VoxPortal;
[code]....
I'm working on a banking program that is supposed to use 3 classes (Account-base class, CheckingAccount, and SavingsAccount) and several methods to display the banking information (ID, balance after a withdrawal and deposit, and the annual interest rate). This should be a pretty simple program, but I'm getting hung up on one portion of it. I'm getting some compiler errors, all of which deal with non-static variables being called from a static context (I'll also post these errors following the code). Up until the middle of last week, we just declared everything as static, but that's changed and I'm having trouble figuring out when to and when not to use static when declaring my methods, hence the compiler errors.
import java.util.Date;
public class Account {
private int id = 0;
private double balance = 0;
private double annualInterestRate = 0;
private Date dateCreated = new Date();
[Code] ....
Here are the compiler errors I am receiving:
Compilation completed. The following files were not compiled:
6 errors found:
File: C:UsersHiTechRedneckDesktopSummer II 2014Computer Programming PrincipleProgram 5CheckingAccount.java [line: 7]
Error: non-static method getId() cannot be referenced from a static context
[Code] .....
I am trying to call an actionListener which is shown below in my PSVM :
class testMenuItemListener implements ActionListener
{
public void actionPerformed(ActionEvent arg0) {
getContentPane().removeAll();
createPanel();
getContentPane().add(panel1); //Adding to content pane, not to Frame
repaint();
[Code] .....
I get the following error :
Frame.java:409: error: non-static variable this cannot be referenced from a static context
menuItem1.addActionListener(new testMenuItemListener());