Java Object Declaration Beyond Common Idiom
Feb 5, 2015
when a new object is created in Java it follows the idiom:
Object obj = new Object();
where the Object() constructor matches the object type Object.
But what if it doesn't? I understand from the Oracle Docs on creating objects and polymorphism that the constructor must be in that object's class or one of its subclasses. However, suppose we wanted to declare a new stack. My first instinct would be:
Stack s1 = new Stack();
But I assume it's valid to do it this way, too:
Object s2 = new Stack(); // Is there a difference here? What are we really saying about s2? I'm guessing s2 is simply an empty stack, but only has access to the Object class methods? I'm not sure why someone would ever do this, but I want to solidify my understanding of the Java hierarchy. Are there really any circumstances where someone would use a subclass's constructor when creating a new object?
View Replies
ADVERTISEMENT
Mar 10, 2015
I have an application that uses an object that is declared globally for the class. Within a method that is triggered by a user event, it creates a new object and assigns it to this global object declaration. My question is, when the object is overwritten multiple times by the user selecting the button that calls this method, will the older instances be garbage collected or is there still a reference to them? Is there any downfall to this logic and if so what would be a solution.
View Replies
View Related
Aug 18, 2014
I have two different "business objects", and they have multiple attributes in common(around 25 I believe, all of which are simply a String). Basically, these objects are used for documentation purposes on the same file.
The program can choose to update a given Document at any point in time, even if changes haven't been made to existing version. So what I'm trying to do, is check to see if these attributes differ any between the two files(the exisitng copy, and the new request). If so, I'll update...else I simply throw out the request. The workload can be rather intense at times so I don't want to bog down the system anymore then necessary.
Simply pulling down every attribute for each and comparing seems like a lot of overhead, any more efficient way to achieve these results?
View Replies
View Related
Jan 29, 2014
"What happens if you modify the common object references in these lists, myArrList and urArrList ? We have 2 cases here: In the first one, you reassign the object reference using either of the lists. In this case, the value in the second list will remain unchanged.In the second case, you modify the internals of any of the common list elements - in this case, the change will be reflected in both lists."
I have written the following code, which tests the first case mentioned above, and i get the output as expected: myarrList remains unchanged. How can i test the second case ? My thoughts are ....'second case is untestable the following code, because String is immutable. I need to use StringBuilder or something else to write code for test of second case mentioned'.
ArrayList<String> myarrList = new ArrayList<>();
myarrList.add("one");
myarrList.add("two");
ArrayList<String> urarrList = new ArrayList<>();
urarrList.add("three");
urarrList.add("four");
System.out.println("ArrayLists setup");
[code]....
View Replies
View Related
Oct 26, 2014
Netbeans tells me it's an illegal start of expression during the initialisation of the interactWithUser method.
public class InvertLetter {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
/**
* String mit den Kleinbuchstaben.
*/
final String lowercase = "abcdefghijklmnopqrstuvwxyz";
[Code] ....
View Replies
View Related
Apr 9, 2015
Let's say within a class I create a method that takes care of creating a java swing layout with labels, buttons etc.. then attach an action listener (inner class) for each button to change a respective label text. All I would need is that the action listener method can access and modify the label as needed.
Have read about static, protected, private, getters and setters but honestly bit confused about which structure should be adopted as a best practice. Global static protected variables for the labels along with private inner classes implementing ActionListeners believe will do the trick and will be able to access the labels but not convinced this is good practice.
View Replies
View Related
Apr 30, 2015
My Question is Suppose I am declaring an String array as
String[] a
or
String a[]
or
Comparable[] a
Will there be any performance or other advantage or disadvantage using these syntax?
View Replies
View Related
Feb 26, 2015
import java.util.HashSet;
import java.util.ArrayList;
public class Graph
{
double [] [] adj;
graph (double [] [] a)
{
adj= new double [a.length][a.length];
for (int i=0;i<a.length;i++)
for (int j=0;j<a.length;j++)
adj[i][j]=a[i][j];
}
C:UserscDesktopGraph.java:: error: invalid method declaration; return type required
graph (double [] [] a)
View Replies
View Related
Feb 13, 2014
I tried this:
String[][]f = new String[1][1] {{"Harry"}{"Hairy"}};
I also tried this:
String[][]f = new String[1][1] {{"Harry"},{"Hairy"}};
but I get an error
View Replies
View Related
Apr 13, 2014
Ok say you have
public class MyClass {
private int x = 5;
Object myObject = new Object();
public MyClass(){
}
}
When would the myObject be created? Before or after the constructor? And does this mean you can't pass
this into the parameters of that object?
View Replies
View Related
Feb 27, 2014
I'm new to Java and have been stuck on how to use a final declaration statement once it's made. Below is a class I'm creating with the intention of calling it under a main method. I don't understand if I'm supposed to do anything else, like do some sort of get/set, or if the final static line is all I need. And, I don't know how I call it to the main method once I do.
public class Shirt//class name.
{
int collarSize;//data field.
int sleeveLength;//data field.
public final static String MATERIAL = "cotton";//final data field for material.
[Code] ....
View Replies
View Related
Dec 23, 2014
I started learning JSTL (Java Standard Tag Library) and i got to know that i need to mention this directive in JSP page
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
in order to use JSTL tags.
I am not able to understand what does this URI exists at all ?? I feel it making things complex and meaningless or else i am missing something hidden and secret.
View Replies
View Related
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
Sep 3, 2014
I have one interface with three(more than one) method declaration. In the subclass that implements it I want to define only one method not all three not even blank definition of them.Is there any keyword or method for that. How to do it? Is it possible to do it? In GUI we use adapter classes to achieve it. What for console application?
View Replies
View Related
Jan 30, 2014
I have doubts in string declaration. As I know we can declare string in two ways:
1. String a=new String("Hello");
2. String b="Hello";
What is exact difference between them? Another thing is when I check (a==b) it retuns me false, but when I check a.equals(b) it returns me with true. Why So?
View Replies
View Related
Jan 29, 2015
I know that I can declare and initialize an member variable inside a class in a single statement but I can't do them on separate sentence. Why?
For example, I am allowed to do the following,
class A{
int a = 5;
// rest of the class
}
But I am not allowed to do this,
class A{
int a;
a = 5; // it doesn't compile even when the variable 'a' is static
// rest of the class
}
Why java don't allow me to do that?
View Replies
View Related
Jun 29, 2014
How to use common functions and constants in one jsp page which are used in another jsp page on page load time?
View Replies
View Related
May 12, 2014
Which of these is not a real differentiator for programming languages:
a) Object-oriented / Process-Oriented
b) Interactive / Automated
c) Interpreted / Compiled
d) Strongly-Typed / Weakly-Typed
e) All of the above
f) B and C
g) B and D
Almost all support OOP, Interactive/Automated, Interpreted/Compiled but not sure about Strongly typed/Weakly typed.
View Replies
View Related
Apr 6, 2015
I have two ArrayLists and I want to compare them for common elements, and based on the result I want to update the first Arraylist to only have these elements. sort of like the opposite of RemoveAll() which removes elements in common and keep the ones that are unique. so far I thought of using for loop and .contains() in case it was fault,element not present, remove from list. but I was wondering
in what other ways, perhaps APIs i can use to do that?
View Replies
View Related
Mar 19, 2014
I need to create an algorithm that finds the common element(s) in all arrays that has a signature of public Comparable[] findCommonElements(Object[] collection) that has an efficiency of at most O(knlogn), uses a query array, and accepts as input a collection of arrays. I am aware my time would be better spent learning how to use array lists and hash sets, but I am supposed to use concepts already covered, and these have not been.
I feel like this code should work, but it is returning null for the array of common elements. Which means it obviously is not working correctly. I am also likely going to need implementing the sort algorithm, but I wanted to get the part of finding the common elements set first.
public class CommonElements2<T extends Comparable<T>>
{
Comparable[] tempArr;
Comparable[] common;
Comparable[] queryArray;
/*
sort algorithm goes here
*/
public Comparable[] findCommonElements(Object[] collections)
[code]....
View Replies
View Related
Mar 16, 2015
I have developed a web portal using jsp and struts 2. I have approximately 10 JSP pages which looks exactly the same and have two text areas and two hidden fields. All 10 pages are exactly the same except for hidden field value. Can't i have a single common jsp page. How can i achieve it. A sample page i am attaching...
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ page pageEncoding="UTF-8"%>
<%@ page language="java"%>
[Code]....
As this is an Assignment page so hidden field value is assignment. If the page is OBJECTIVEs then value will be OBJECTIVE.
View Replies
View Related
Sep 17, 2014
i need to find common String between to Strings :
Example 1:
Customer Name 1: Dr. Joe Smith
Customer Name 2: Joseph Smith, MD.
I need to search the string for a match, in this example "Smith"
Example 2:
Customer Name1: New York Market Place
Customer Name 2: NY Marketplace
I need to search the string for a match, in this example Market place
Example 3:
Customer Name1: The Deli on the Corner
Customer Name 2: Corner Deli
I need to search the string for a match, in this example Deli Corner
View Replies
View Related
May 14, 2015
I am using the following regex - [a-zA-Z0-9]{9,18} which means I can use alphabets and numbers with minimum length as 9 and maximum length as 18.It should not take special characters.
It takes values like ADV0098890 etc. but it is also taking ADV0098890[] which is wrong.
How can I prevent that ?
View Replies
View Related
Jun 3, 2014
I have to implement all the stack methods in java such as push, pop empty, not using the ready methods but have to create them and to execute an exercise but is sth wrong with it
public class Stiva {
/** the problem is here how to declare the stack 1 and stack 2 and kreu(head) gjmax(size)*/
int Gjmax;
int array[] = new int[Gjmax];
int kreu;
private Stiva stiva1;
private Stiva stiva2;
[Code] .....
View Replies
View Related
Oct 16, 2014
After learning about Euclid's Algorithm for finding the greatest common divisor of 2 integers, I decided to make a demo program in Java.
public static int gcd(int number1, int number2) {
int num1 = number1;
int num2 = number2;
if(number1 < number2){
[Code] .....
View Replies
View Related
Feb 7, 2007
import javax.swing.*;
public class mostFrequent{
public static void main(String args[]){
char index;
String s;
s = JOptionPane.showInputDialog("Enter String here");
int currFrequency = 0;
for(index = 0; index<s.length(); index = index++){
int i = 0;
for(i = 'A'; i<='Z'; i++){
if(i==s.charAt(index)){
currFrequency = currFrequency + 1;
}
}
}
System.out.println("end");
}
}
//my code so far
View Replies
View Related