Array Reference Instantiation

May 17, 2014

I am currently learning Java basic concepts and this is a very simple code which produces error depending on the way I write. It is as follows:

package testPackage;
interface ArrayInsideClassShock {
}
class Check{
ArrayInsideClassShock var[]; //Error line
var= new ArrayInsideClassShock[10];
}

Error in eclipse:Syntax error on token ";", , expected

But if I write it as:

package testPackage;
interface ArrayInsideClassShock {
}
class Check{
ArrayInsideClassShock var[]=new ArrayInsideClassShock[10];
}

It doesn't show any error.

View Replies


ADVERTISEMENT

Multi-dimension Array Declaration And Instantiation?

May 17, 2015

Whilst pre-preparing for java certification, one of the online mock exams has slightly confused me by saying my answer was incorrect for multi-dimension array 'declaration and instantiation'.

This is one of the answers i chose - which was marked as incorrect

a)
int[][] array2d = {{123}, {4,5}};

Which looks absolutely fine to me.One of the other answers, which i agree is correct and so does the mock exam is

b)
int [][] array2d = new int[2][2];

View Replies View Related

Generic Instantiation Of A Collection - ArrayList

Nov 18, 2014

So I have a persons class:

public class Person
{
private String name;
private int age;
 public Person (String name, int age) {
this.name = name;
this.age = age;

[Code] .....

And I need to write a simple main method that creates lots of instances of the Person class and adds them to a generic instantiation of a Collection (ArrayList). And I need to make it so I as a programmer can define how many instances to create.

View Replies View Related

Instantiation Efficiency - Declare Variables To Be Used In A Loop

Sep 6, 2014

Where usually to instantiate and declare variables to be used in a loop. If you declare it outside to be used in the loop it will still be there when the loop is done, never to be used again and is just sitting there taking up memory. However, if you declare it inside the loop, you have the issue of it constantly creating space for said variable, but once it's out of scope it's gone. Is there any advantage performance wise to doing it either way?

View Replies View Related

Swing/AWT/SWT :: Instantiation Of Listener Using Anonymous Inner Class

Jun 2, 2014

JMenuItem blueChoice = new JMenuItem("Blue");
blueChoice.addActionListener(new InnerMenuListener(){
public void actionPerformed(ActionEvent e){
String buttonCommandString = e.getActionCommand( );
if (buttonCommandString.equals("Red"))
redPanel.setBackground(Color.RED);
else if (buttonCommandString.equals("White"))
whitePanel.setBackground(Color.WHITE);
else if (buttonCommandString.equals("Blue"))
bluePanel.setBackground(Color.BLUE);
else
System.out.println("Unexpected error.");
}
});

Can a method in an anonymous inner class ever be private? why or why not?

View Replies View Related

How To Reference The Object

Jan 27, 2014

I am doing a Junit test case, but I keep having issues with AssertEquals ( object expected, object actual). I don't know how to reference the other actual object so that it can compare to the expected.

public void add (Distance d) throws CustomException

//I can also convert feet to inches and add all inches together and then divided inches to feet and inches
Distance result = new Distance();
int newFeet = this.feet + d.getFeet();
int newInches = this.inches + d.getInches();
if(newInches > 11)
{
newFeet = newFeet + (newInches/12);

[Code]...

View Replies View Related

Int Is Not A Reference Type

Jan 24, 2015

I don't really know what this means and it is sending an error when I try to run my program I am trying to set the x and y value (Int x and int y) to setVisible false at a specific time in my game but it keeps sending the error that int is not a reference type.

View Replies View Related

Cannot Pass By Reference?

Aug 25, 2014

I'm fairly new to Java, I'm very experienced with C++ and C# in which you can pass by reference - extremely useful. Take for example this bit of code in C#:

class MyClass
{
public MyClass(int i)
{
m_i = i;
}
  public int m_i;

[Code] ...

Just at the end of this program x.m_i will be equal to 8. As far as I can see this is not possible in Java: you can't pass a double by reference, using a Double will kick in the autoboxing so that won't work either. The only "solution" in Java would be to pass in a double[] (of length 1) or to make a wrapper class, both nasty solutions because a user may want to just hold a double as a member of their class just as I have, for reasons such as not allocating more memory for a class and generally not being bloated.

View Replies View Related

How To Reference Action Listeners

Mar 15, 2015

I am making an MVC program and I am not allowed to put the action listeners in the view class. I was able to get one button working fine but since I am unable to reference them I cannot give them both individual responses.

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Controller {
HobbyList model;
ListView view;

[code]....

View Replies View Related

How To Reference Input Into Another Method

Apr 17, 2014

public class {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double[] numbers = new double[10];
System.out.println("Enter " + numbers.length + "numbers");
for (int i = 0; i < numbers.length; i++) {
numbers[i] = input.nextDouble();

[Code] .....

I am trying to reference the input "numbers[i]" to my mean method, how do i do that?

View Replies View Related

Java And Passing By Reference

Nov 12, 2014

I know in C++ It's possible to pass by reference but what about java?

For example, can i pass the address of a health variable into a Ninja class and then

Since Ninja inherits from an enemy class pass the health into the enemy class and within there have a function that returns that same health address and takes 5 from the health returning 95.

View Replies View Related

Reference Of Abstract Class

Aug 29, 2014

I passed my abstract class private final reference to another concrete class and I used abstract class reference as parameter to that concrete class constructor and in my main method and null to that parameter then only that program executes correctly...i placing my code below ..if there is any error tell me where is error occurring then i will check my code...i think my code is right but little bit doubt abstract class concept.

{
}
class concept1 extends concept
{
private final concept parent;
public concept1(concept aparent)
{
parent=aparent;
System.out.println(parent);
}
public static void main(String args[])
{
//concept p=new concept1(null);
concept c=new concept1(null);
}}

View Replies View Related

Purpose Of Type Associated With A Reference?

Mar 29, 2014

When I write a statement such as:

myType x;

This means that x is a reference to objects of type, "myType" and no memory has been set aside for an instance of this object. I think it's true that x also has the starting memory address of this object.

What I feel uncertain about is if x is telling the JVM(?) where it can find the starting address of this object, why do we care about the type that x is?

i.e., if we assign x to an object totally unrelated to this type then the compiler would complain - correct? If so, how come, since all x is doing is telling the JVM(?) where to find your object (via the address it contains).

I suppose all, hy a reference has a type associated with it if all the reference does is point you to the object?

View Replies View Related

Reference Variable Concepts

Feb 15, 2010

what these mean with examples..

1. A reference variable can be of only one type and once declared, that type can never be changed(although the object it references can change)

2. A reference is a varaible ,so it can be reassigned to other objects(unless the reference is declared final)

3. A reference variable's type determines the methods that can be invoked on the object the variable is referencing.

4. A reference variable can refer to any object of the same type as the declared reference , or - it can refer to any subtype of the declared type.

5. A reference variable can be declared as a class type or an interface type. If the variable is declared as an interface type, it can reference any object of any class that implements the interface.

View Replies View Related

String Reference Equality

Jan 23, 2014

I don't understand why following code when executed returns false
 
String s1= " string".trim();
  if(s1 == "string"){
  System.out.println("true");
  }
  else{
  System.out.println("false");
  }

View Replies View Related

Checking Reference That Is Not Duplicate

Jan 29, 2014

How can i check the reference that is not duplicate and date format is DDDCCYYnnnnnnn, CC must be 20 and YY must not be less than the current year, nnnnnnn is the sequetial number of the file and to complete the 7 numeric characters, zero's must be populated in front of the number.  DDD must be a valid reporting entity code. there must not be a duplicate. Code I try

private boolean test-date()
    {
        String s_Ref = this.transactionFile.getsReference();
        boolean flag = true;
        if(!isValidPattern(s_Ref, "[0-9]{14}"))

[Code] ....

The reference number is like 201401030234509

If there is duplicate show error code 105

View Replies View Related

Final Reference Variables In Java

Feb 28, 2015

I am unable to understand the meaning of this sentence "final reference variables must be initialized before the constructor completes.",What is trying to imply?

View Replies View Related

How To Reference Object Created In One Class From Another

Oct 30, 2014

I have started working on a little project in my free time. It is just a simple text rpg that runs in a counsel window. I have 5 files each file contains 1 class.

public class SomnusCharacter {
private String gender = "";
private int age = 0;
private String race = "";
private int level = 0;
private int xp = 0;

[Code] ....

The chain of events right now is:

1. MainMenu is run
2. If user inputs n CreateCharactor is run
3. User inputs name, age, ect in SomnusCharacter object made in CreateCharacter
4. Intro (just rough demo for testing purposes) is run
5. If user inputs m Menu is run
6. Menu calls and prints out all the information from the object made in CreateCharacter

Step 6 is where I am having my problems. How can I reference (lets say the SomnusCharacter object made is called player) player from my Menu class? I know that if I made a new character that it would just create another SomunsCharacter object with the default values again.

View Replies View Related

Class That Needs To Be Copied Also Contains Reference To Other Object

May 22, 2014

I read somewhere : "Java use clone() method of Object class to copy content of one object to the other. The problem will arrive if the Class that needs to be copied also contains reference to the other object."Not able to understand the second line.

View Replies View Related

JSF :: Reference Variable Passed To A File

Sep 30, 2014

How are methods invoked on the `bird` parameter?

next.xhtml:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">

[Code] ....

`MyQueueBean` is intended to give out a bird once only, to exactly one end-user. Because it's application scoped, and not session scoped, getting attributes directly from the bean would give inconsistent results.

The birds application is from Facelets Essentials Guide to JavaServer Faces View Definition Framework: [URL] ....

how is the variable passed to the file? Once it's passed, how is it referenced?

View Replies View Related

Polymorphic Reference Doesn't Seem To Be Specific

May 18, 2015

Is there a short term that means: "using an object reference with the datatype of a supertype to refer to an object with the datatype of a subtype"?

Saying "polymorphic reference" doesn't seem to be specific enough because ALL references other than those with the Object datatype are polymorphic. I saw one post that referred to these reference variables as a "supertype reference." In the absence of any other (more official) term, I may just use that term because calling a reference a "supertype reference" implies that there is a subtype. It sure beats what I was thinking about using: "sub-as-super." Which, when the inevitable mispronunciations occur, would lead to all kinds of off-color jokes.

Is there an "official" term that all of the dozens of Java books I have read seem to have missed? Or does everyone stumble along, spelling things out with a nearly sentence-long phrase.

View Replies View Related

JSP :: Get Child Of Anchor Tag Forwarded To Hyper Reference

May 10, 2014

Is there a way to forward the text child of an anchor tag to its hyper reference?

For example, suppose I have an HTML file:

<a href = "ABC.jsp">SomeText</a>

Is there a way to send text 'SomeText' to ABC.jsp so it can be processed?

View Replies View Related

JSP :: How To Give Reference Of JavaScript And CSS File In Page

Aug 14, 2014

I have just started to work on Java EE and I am not able to put the refrence of css and javascript in jsp page.

My senario is below: I have created script file inside myJavaScript Folder like Web Container-> myJavaScript ->Home.js. Similarlly for css:Web Container-> myCss ->Home.css.

My Jsp page palced inside Like: Web Container-> Home ->Home.jsp.

How to give the reference of js and css file in my Home.jsp page.

View Replies View Related

Possibility To Use Pass By Reference Technique In Java

Aug 27, 2014

if there is a possibility to use the pass by reference technique in java like in C++.URL....

View Replies View Related

EJB / EE :: Pass By Reference Not Working Between Remote Calls

Mar 11, 2014

i'm having issues with pass by reference between remote EJB method calls. Below is what i'm trying to do. The EJB method calls are made using remote (EJBObject) interface.

insertEmployeeRecord(Employee employee) {
employeeEJB.insertEmployee(employee);
empDetailsEJB.insertEmpDetails(employee);
}
insertEmployee(Employee employee)

[Code] ....

All the EJBs have below configuration in the weblogic-ejb-jar.xml file.

<enable-call-by-reference>true</enable-call-by-reference>

I'm using Weblogic server 10.3.0.4 and JDK160_21.

View Replies View Related

Methods Checked Based On Reference Not The Object?

Jun 15, 2014

Check out the following basic code (assume that dog has a method called bark):

Dog d = new Dog();
Object o = d;
 
o.bark(); // error

But why? Isn't o just a pointer to a memory address which has the dog object? If so, why can't the compiler see that the object has a method called bark? Or, to ask the question another way, why is Java designed to check the object reference to see if the method exists instead of the object itself?

View Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved